Notes Aws
Notes Aws
Notes Aws
--group-name HelloWorld \
--vpc-id vpc-4cddce2a
--group-name HelloWorld \
--protocol tcp \
--port 22 \
--cidr 0.0.0.0/0
--group-name HelloWorld \
--protocol tcp \
--port 3000 \
--cidr 0.0.0.0/0
--group-names HelloWorld \
--output text
"KeyPairs": [
"KeyName": "EffectiveDevOpsAWS",
"KeyFingerprint":
"27:83:5d:9b:4c:88:f6:15:c7:39:df:23:4f:29:21:3b:3d:49:e6:af"
$ cat ~/.ssh/EffectiveDevOpsAWS.pem
MIIEpAIBAAKCAQEAiZLtUMnO2OKnHvTJOiIP26fThdsU0YRdlKI60in85x9aFZXSrZsKwOhWPpMtnUMJKeGv
VQut+gJ1I1PNNjPqS2Dy60jH55hntUhrArpaL2ISDX4BgRAP1jcukBqS6+pL+mTp6OUNTToUt7LvAZoeo+10S
YbzHF1ZMQLLs96fCMNvnbJdUCa904dJjJs7t/
G2ou9RiNMRx8midrWcmmuGKOb1s6FgrxJ5OAMYegeccFVfGOjqPk3f+6QTPOTMNgNQ8ANKOMA9YtcIca
/75QGUPifusTqUT4Fqtv3rbUYPvacAnYL9eCthtn1XMG7Oo/
mR5MrU60wib2QcPipmrGNbwIDAQABAoIBABSyqkmxUxGGaCZcJbo9Ta16fnRxFZzAEWQ/
VCIydv4+1UrSE7RS0zdavT8E3aP/Ze2LKtncu/
wVSpJaFVHGVcWpfGKxvIG3iELZ9oUhDyTWx3+IKanFRNyxyKudk+UyhuPRMu/
7JhksV9mbbiILkfiPzSMSzpjB4p1hEkypfbvBnrbB+sRycx+jK5l209rNDukkJVvyFCnqPiH0wmvKRqHTNOMGW
mM6CPOU+VpuMX+dIlrSeId7j6hqMjA0rGncnxYi035v2zicvIsEKHZ9MZCnkiRb3kJ9PhueTwwUQmoBYfV5E
+1Wu34UmdsmALQEX3xniaR6xf9iWhQ2Nh8LaECgYEAzXHOZDPAUzXitO735KBUaiBp9NMv2gzE862Yf2r
mDkFM4Y5RE3DKHrKfeOkrYqlG11On0m44GHBk/g4eqqIEaBjVp6i/
Lk74tpQU6Kn1HT3w9lbXEFsCWjYZnev5oHP6PdedtRYNzZsCSNUdlw0kOG5WZZJ4E7mPZyrvK5pq+rMCgY
EAq22KT0nD3d59V+LVVZfMzJuUBDeJeD139mmVbzAq9u5Hr4MkurmcIj8Q6jJIQaiC8XC1gBVEl08ZN2oY1
+CBE+Gesi7mGOQ2ovDmoTfYRgScKKHv7WwR+N5/
N7o26x+ZaoeaBe43Vjp6twaTpKkBOIuT50tvb25v9+UVMpGKcFUC
gYEAoOFjJ3KjREYpT1jnROEM2cKiVrdefJmNTel+RyF2IGmgg+1Hrjqf/
OQSH8QwVmWK9SosfIwVX4X8gDqcZzDS1JXGEjIB7IipGYjiysP1D74myTF93u/
16qD89H8LD0xjBTSo6lrn2j9tzY0eS+Bdodc9zvKhF4kzNC4Z9wJIjiMCgYAOtqstXP5zt5n4hh6bZxkL4rqUlhO1
f0khnDRYQ8EcSp1agh4P7Mhq5BDWmRQ8lnMOuAbMBIdLmV1ntTKGrN1HUJEnaAEV19icqaKR6dIlSFYC4
stODH2KZ8ZxiQkXqzGmxBbDNYwIWaKYvPbFJkBVkx1Rt9bLsKXpl/
72xSkltQKBgQCYEjUVp4dPzZL1CFryOwV72PMMX3FjOflTgAWr8TJBq/
OLujzgwYsTy6cdD3AqnMQ2BlU7Gk4mmDZCVVsMqHFbIHEa5Y4e5qIQhamedl3IgmnMpdyuDYaT/
Uh4tw0JxIJabqm+sQZv4s1Otgh00JlGrgFs+0D39Fy8qszqr6J04w==
--instance-type t2.micro \
--key-name EffectiveDevOpsAWS \
--security-group-ids sg-01864b4c \
--image-id ami-cfe4b2b0
SystemStatus
SYSTEMSTATUS ok
--instance-ids i-057e8deb1a4c3f35d \
--query "Reservations[*].Instances[*].PublicDnsName"
$ ssh -i ~/.ssh/EffectiveDevOpsAWS.pem ec2-user@ ec2-34-201-101-26.compute-1.amazonaws.com
v0.10.48
[ec2-user@ip-172-31-22-52 ~]$
wget https://raw.githubusercontent.com/yogeshraheja/Effective-DevOps-with-AWS/master/
Chapter02/helloworld.js -O /home/ec2-user/helloworld.js
151.101.200.133
/home/ec2-user/helloworld.js 100%
[====================================================================================
=100%
[====================================================================================
=>] 384 --.-KB/s in 0s
Server running
Ctrl + C
etc/init/helloworld.conf
respawn script
end script
[ec2-user@ip-172-31-22-52 ~]$
/etc/init/helloworld.conf 100%
[====================================================================================
=>] 301 --.-KB/s in 0s
[ec2-user@ip-172-31-22-52 ~]$
[ec2-user@ip-172-31-22-52 ~]$
helloworld stop/waiting
instance-id: i-057e8deb1a4c3f35d
logout
-----------------------------------
Collecting setuptools
Downloading
https://files.pythonhosted.org/packages/ff/f4/385715ccc461885f3cedf57a41ae3c12b5fec3f35cce4c870
6b1a112a133/setuptools-40.0.0-py2.py3-none-any.whl (567kB)
Uninstalling setuptools-0.9.8:
helloworld-cf-template.py
Base64,
ec2,
GetAtt,
Join,
Output,
Parameter,
Ref,
Template,
ApplicationPort = "3000"
t = Template()
t.add_parameter(Parameter(
"KeyPair",
Type="AWS::EC2::KeyPair::KeyName",
))
t.add_resource(ec2.SecurityGroup(
"SecurityGroup",
access".format(ApplicationPort),
SecurityGroupIngress=[
ec2.SecurityGroupRule(
IpProtocol="tcp",
FromPort="22",
ToPort="22",
CidrIp="0.0.0.0/0",
),
ec2.SecurityGroupRule(
IpProtocol="tcp",
FromPort=ApplicationPort,
ToPort=ApplicationPort,
CidrIp="0.0.0.0/0",
),
],
))
***http://amzn.to/1VU5b3s
ud = Base64(Join('\n', [
"#!/bin/bash",
"start helloworld"
]))
...
t.add_resource(ec2.Instance(
"instance",
ImageId="ami-cfe4b2b0",
InstanceType="t2.micro",
SecurityGroups=[Ref("SecurityGroup")],
KeyName=Ref("KeyPair"),
UserData=ud,
))
...
***Fn::GetAtt
***GetAtt()
...
t.add_output(Output(
"InstancePublicIp",
Value=GetAtt("instance", "PublicIp"),
))
t.add_output(Output(
"WebUrl",
Description="Application endpoint",
Value=Join("", [
":", ApplicationPort
]),
))
...
print t.to_json()
***link: https://raw.githubusercontent.com/yogeshraheja/Effective-DevOps-with-AWS/master/
Chapter03/EffectiveDevOpsTemplates/helloworld-cf-template-part-1.py
$ python helloworld-cf-template.py >helloworld-cf.template
***documentation at http://bit.ly/1W6s96M
$ git clonehttps://github.com/<your_github_username>/EffectiveDevOpsTemplates
$ cd EffectiveDevOpsTemplates
EffectiveDevOpsTemplates
$ cp <path_to_helloworld_template>/helloworld-cf-template.py
$ git push
***https://api.ipify.org
...
Base64,
ec2,
GetAtt,
Join,
Output,
Parameter,
Ref,
Template,
ApplicationPort = "3000"
PublicCidrIp = str(ip_network(get_ip()))
...
SecurityGroupIngress=[
ec2.SecurityGroupRule(
IpProtocol="tcp",
FromPort="22",
ToPort="22",
CidrIp=PublicCidrIp,
),
....
https://github.com/yogeshraheja/Effective-DevOps-with-AWS/blob/master/Chapter03/
EffectiveDevOpsTemplates/helloworld-cf-template.py.
46c46
<"CidrIp":"54.164.95.231/32",
---
>"CidrIp": "0.0.0.0/0",
91a92
>
--group-names HelloWorld-SecurityGroup-1XTG3J074MXX
***change sets
$ git commit -am "Only allow ssh from our local IP"
$ git push
***https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-the-
control-machine.)
$ ansible --version
ansible 2.6.2
u'/usr/share/ansible/plugins/modules']
packages/ansible
--stack-name ansible \
--template-body file://helloworld-cf-v2.template \
--parameters ParameterKey=KeyPair,ParameterValue=EffectiveDevOpsAWS
"StackId": "arn:aws:cloudformation:us-east-
1:094507990803:stack/ansible/bb29cb10-9bbe-11e8-9ee4-500c20fefad2"
$ cd ansible
$ chmod +x ec2.py
***ec2.ini
[ec2]
regions = all
rds = False
$ ./ec2.py
***ansible.cfg
[defaults]
inventory = ./ec2.py
remote_user = ec2-user
become = True
become_method = sudo
become_user = root
nocows = 1
http://bit.ly/24rU0yk
$ ansible-doc <Module-Name>
$ ansible-doc ping
"changed": false,
"ping": "pong"
------------
$HOME/.ssh/config
IdentityFile ~/.ssh/EffectiveDevOpsAWS.pem
PasswordAuthentication no
ForwardAgent yes
-a 'df -h'
***playbook
***https://github.com/ansible/ansible-examples
***https://github.com/yogeshraheja/Automation-with-Ansible-By-Yogesh-Raheja
***http://bit.ly/1ZqdcLH
$ mkdir roles
$ cd roles
***https://galaxy.ansible.com/
$ cd nodejs
---
with_items:
nodejs
npm
***https://github.com/yogeshraheja/Effective-DevOps-with-AWS/blob/master/Chapter03/ansible/
roles/nodejs/tasks/main.yml
$ cd ..
$ cd helloworld
***files
tasks/main.yml
---
mode: 0644
***http://bit.ly/1WBv08E
src: helloworld.conf
***//bit.ly/22I7QNH
***https://github.com/yogeshraheja/Effective-DevOps-with-AWS/blob/master/Chapter03/ansible/
roles/helloworld/tasks/main.yml
handlers/main.yml
---
# handlers file for helloworld
meta/main.yml
dependencies:
- nodejs
***https://github.com/yogeshraheja/Effective-DevOps-with-AWS/blob/master/Chapter03/ansible/
roles/helloworld/meta/main.yml
helloworld.yml
---
roles:
- helloworld
***https://github.com/yogeshraheja/Effective-DevOps-with-AWS/tree/master/Chapter03/ansible
***---
foo
bar
baz
ansible-playbook <playbook.yml>[options]
$ ansible-playbook helloworld.yml \
--private-key ~/.ssh/EffectiveDevOpsAWS.pem \
-e target=ec2 \
--list-hosts
playbook: helloworld.yml
pattern: [u'ec2']
hosts (1):
18.206.223.199
dry-run
$ ansible-playbook helloworld.yml \
--private-key ~/.ssh/EffectiveDevOpsAWS.pem \
-e target=18.206.223.199 \
--check
PLAY [18.206.223.199]
*************************************************************************************
*************************************************************
ok: [18.206.223.199]
TASK [nodejs : Installing node and npm]
*************************************************************************************
*******************************************
changed: [18.206.223.199]
changed: [18.206.223.199]
*************************************************************************************
**************************
changed: [18.206.223.199]
changed: [18.206.223.199]
PLAY RECAP
*************************************************************************************
************************************************************************
$ ansible-playbook helloworld.yml \
--private-key ~/.ssh/EffectiveDevOpsAWS.pem \
-e target=18.206.223.199
$ curl 18.206.223.199:3000
Hello World
$ git push
***canary testing
roles/helloworld/files/helloworld.js
}).listen(3000);
$ ansible-playbook helloworld.yml \
--private-key ~/.ssh/EffectiveDevOpsAWS.pem\
-e target=18.206.223.199 \
--check
$ ansible-playbook helloworld.yml \
$ ansible '18.206.223.199' \
--private-key ~/.ssh/EffectiveDevOpsAWS.pem \
--become \
$ ansible '18.206.223.199' \
--private-key ~/.ssh/EffectiveDevOpsAWS.pem \
--become \
localhost
[localhost]
localhost
ansible_connection=local
$ git push
$ ansible '18.206.223.199' \
--private-key ~/.ssh/EffectiveDevOpsAWS.pem\
-m cron -a 'name=ansible-pull minute="*/10" job="/usr/bin/ansible-pull -U
https://github.com/<your_username>/ansible helloworld.yml -i localhost --sleep 60"'
$ curl 54.175.86.38:3000
Hello World
ansiblebase-cf-template.py.
$ cd EffectiveDevOpsTemplates
$ cp helloworld-cf-template.py ansiblebase-cf-template.py
ansiblebase-cf-template.py
ApplicationName = "helloworld"
ApplicationPort = "3000"
ApplicationPort = "3000"
GithubAccount ="EffectiveDevOpsWithAWS"
GithubAnsibleURL = "https://github.com/{}/ansible".format(GithubAccount)
AnsiblePullCmd = \
ApplicationName
ud = Base64(Join('\n', [ "#!/bin/bash",
"yum install --enablerepo=epel -y git", "pip install ansible",
AnsiblePullCmd,
]))
***//github.com/yogeshraheja/EffectiveDevOpsTemplates/blob/master/ansiblebase-cf-template.py
--stack-name ansible \
--template-body file://ansiblebase.template \
--parameters ParameterKey=KeyPair,ParameterValue=EffectiveDevOpsAWS
"StackId": "arn:aws:cloudformation:us-east-1:511912822958:stack/HelloWorld/ef2c3250-6428-11e7-
a67b-50d501eed2b3"
--stack-name helloworld \
--template-body file://ansiblebase.template \
--parameters ParameterKey=KeyPair,ParameterValue=EffectiveDevOpsAWS
"StackId": "arn:aws:cloudformation:us-east-
1:094507990803:stack/helloworld/5959e7c0-9c6e-11e8-b47f-
50d5cd26c2d2"
}
$ aws cloudformation wait stack-update-complete \
--stack-name ansible
--stack-name ansible \
--query 'Stacks[0].Outputs[0]'
"OutputKey": "InstancePublicIp",
"OutputValue":"35.174.138.51"