Skip to content

Instantly share code, notes, and snippets.

@thapakazi
Last active August 29, 2019 22:06
Show Gist options
  • Save thapakazi/ce94c2d994d2d845c5512ef5010400ba to your computer and use it in GitHub Desktop.
Save thapakazi/ce94c2d994d2d845c5512ef5010400ba to your computer and use it in GitHub Desktop.
create aws launch configs with ansible ec2_lc module

Agenda

To automate the autoscaling launch config generation process.

if its boring with 🐁 clicks and uis, lets automate it with ⌨ --randomthought

Conventions/Assumptions

convention <100>
userdata.sh holds the script that runs as cloud inti script
vars*.yml the variables necessary to generate configs are defined there
vpc security groups they must be the real values like (sg-xxxxxx) (yes 😏 weird, why you no use names)
assumptions
ansible-version 2.2.0.0

Sample run example

check the sample run block...

Learnings

  • the moduel ec2lc is idempotent (that means, it will give no damn how many times you run it. changed: false, yes that will get)

  • docs 🏁 needs improvement (helps are scattered in so many google-groups discussion links like )

- hosts: localhost
connection: local
vars_files:
- "vars_{{SCALING_RULE_FOR|default('bg_worker')}}.yml"
tasks:
- ec2_lc:
image_id: "{{ami_id}}"
instance_type: "{{instance_profile}}"
name: "{{launch_config_name}}"
instance_profile_name: "{{iam_role}}"
user_data: "{{ lookup('file', 'user_data.sh') }}"
classic_link_vpc_id: "{{vpc_id_to_link}}"
classic_link_vpc_security_groups: "{{vpc_sgs}}"
# volumes:
# - device_name: /dev/sda1
# device_type: standard
# delete_on_termination: true
# - device_name: /dev/sdb
# ephemeral: ephemeral0
security_groups: "{{classic_groups}}"
$ ansible-playbook ec2_auto_scaling_launch_gen_with_ansible.yml -vvvv
Using /etc/ansible/ansible.cfg as config file
[WARNING]: Host file not found: /etc/ansible/hosts
[WARNING]: provided hosts list is empty, only localhost is available
Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc
_________________________ ______________________________
< PLAYBOOK: ec2_auto_scaling_launch_gen_with_ansible.yml >
--------------------------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
1 plays in ec2_auto_scaling_launch_gen_with_ansible.yml
__________________
< PLAY [localhost] >
------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
______________
< TASK [setup] >
--------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
...
ok: [localhost]
_______________
< TASK [ec2_lc] >
---------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
...
ok: [localhost] => {
"arn": "arn:aws:autoscaling:us-east-1:<<MY_USER_ID>>:launchConfiguration:<<some_random_hash>>:launchConfigurationName/configuration-name-to-put-generated-v0.0.1",
"changed": false,
"created_time": "2016-12-09 07:22:18.035000",
"image_id": "ami-id-string",
"instance_type": "c3.xlarge",
"invocation": {
"module_args": {
"assign_public_ip": null,
"associate_public_ip_address": null,
"aws_access_key": null,
"aws_secret_key": null,
"classic_link_vpc_id": "vpc-id-string",
"classic_link_vpc_security_groups": [
"sg-string-vpc-specific-abcdef",
"sg-string-vpc-specific-ghijkl"
],
"ebs_optimized": false,
"ec2_url": null,
"image_id": "ami-id-string",
"instance_monitoring": false,
"instance_profile_name": "my_kickass_iam_role",
"instance_type": "c3.xlarge",
"kernel_id": null,
"key_name": null,
"name": "configuration-name-to-put-generated-v0.0.1",
"profile": null,
"ramdisk_id": null,
"region": null,
"security_groups": [
"my_sg_001",
"my_sg_010",
"my_sg_100"
],
"security_token": null,
"spot_price": null,
"state": "present",
"user_data": "long string in escaped form...of user_data.sh below"
"validate_certs": true,
"volumes": null
},
"module_name": "ec2_lc"
},
...
]
}
____________
< PLAY RECAP >
------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
localhost : ok=2 changed=0 unreachable=0 failed=0
#!/bin/bash
export BUCKET_NAME="my-secure-s3-bucket"
export PRIVATE_KEY_PATH_IN_S3="${BUCKET_NAME}/id_rsa" #my private key
export SSH_CONFIG_PATH_IN_S3="${BUCKET_NAME}/config" #my ssh config for github
export LATEST_REVISION_PATH_IN_S3="${BUCKET_NAME}/pull_revision.sh" # my custom script to pull the latest deployed release
export SSH_DIR="/thapakazi/.ssh"
export SSH_CONFIG_LOCAL="${SSH_DIR}/config"
export PRIVATE_KEY_LOCAL="${SSH_DIR}/github_rsa"
export LATEST_REVISION_LOCAL="/usr/local/bin/pull_revision"
export REGION="us-east-1"
# pull the private key
aws s3 cp s3://${PRIVATE_KEY_PATH_IN_S3} ${PRIVATE_KEY_LOCAL} --region ${REGION}
chmod 400 ${PRIVATE_KEY_LOCAL}
# pull the config file for github
aws s3 cp s3://${SSH_CONFIG_PATH_IN_S3} ${SSH_CONFIG_LOCAL} --region ${REGION}
chmod 600 ${SSH_CONFIG_LOCAL}
# pull latest deployed hash of the application
aws s3 cp s3://${LATEST_REVISION_PATH_IN_S3} ${LATEST_REVISION_LOCAL} --region ${REGION}
chmod 755 ${LATEST_REVISION_LOCAL}
# pull the playbook and run
export SCALEBOOKS_URL="github.com:thapkazi/autoscale"
export SCALEBOOKS_DIR="/thapakazi/playbooks"
ansible-pull -d ${SCALEBOOKS_DIR} -i 'localhost' -U git@${SCALEBOOKS_URL}.git --accept-host-key bootstrap.yml
---
instance_profile: "c3.xlarge"
vpc_id_to_link: "vpc-id-string"
ami_id: "ami-id-string"
launch_config_name: "configuration name to put generated v0.0.1'
classic_groups:
- "my_sg_001"
- "my_sg_010"
- "my_sg_100"
vpc_sgs:
- "sg-string-vpc-specific-abcdef"
- "sg-string-vpc-specific-ghijkl"
iam_role: "my_kickass_iam_role"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment