Skip to content

Instantly share code, notes, and snippets.

@mmasko
Last active October 20, 2021 01:41
Show Gist options
  • Save mmasko/66d34b651642525c63cd39251e0c2a8b to your computer and use it in GitHub Desktop.
Save mmasko/66d34b651642525c63cd39251e0c2a8b to your computer and use it in GitHub Desktop.
Configure cfn-hup, cloudformation tools on ubuntu 18. Based on a gist from https://gist.github.com/kixorz/10194688. Written in YAML.
#This script will install the cloudformation helper work on Ubuntu 18.
#Some values are hard coded. Make sure to update where needed, or add to the parameters section.
#This would probably work on other distros, but I have not tested yet. Try it out.
#Just make sure to change things like apt to yum if trying on another OS.
Parameters:
EnvironmentSize:
Type: String
Default: t3.nano
AllowedValues:
- t3.nano
- t3.small
- t3.medium
Description: Select instance size
#KeyPair section will pull a list of existing keypairs from your account to choose from. Change Default so the intended key is referenced if you are programmatically initiating this build.
KeyPair:
Default: somekey
Description: Existing keypair
Type: AWS::EC2::KeyPair::KeyName
Resources:
#If you change the name of this EC2 instance resource ID from EC2, make sure to update the name throughout the metadata. Anywhere that says EC2 would need to be changed to reflect the new resource instance ID.
EC2:
Type: 'AWS::EC2::Instance'
Properties:
ImageId: ami-063aa838bd7631e0b
InstanceType: !Ref EnvironmentSize
KeyName: !Ref KeyPair
Tags:
- Key: Name
Value: ubuntuCFinit
UserData:
Fn::Base64: !Sub |
#!/bin/bash
apt-get update -y
apt-get install -y python-pip
apt-get install -y python-setuptools
mkdir -p /opt/aws/bin
python /usr/lib/python2.7/dist-packages/easy_install.py --script-dir /opt/aws/bin https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource EC2 --configsets setup --region ${AWS::Region}
/opt/aws/bin/cfn-signal -e --stack ${AWS::StackName} --resource EC2 --configsets setup --region ${AWS::Region}
Metadata:
# If you change the configSet ID from setup, ensure you update it throughout the this resource configuration. Anywhere is says setup, change it to reflect the new ID.
AWS::CloudFormation::Init:
configSets:
setup:
- "configure_cfn"
configure_cfn:
files:
/etc/cfn/cfn-hup.conf:
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
verbose=true
interval=5
mode: "000400"
owner: root
group: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.EC2.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource EC2 --configsets setup --region ${AWS::Region}
mode: "000400"
owner: root
group: root
/lib/systemd/system/cfn-hup.service:
content: !Sub |
[Unit]
Description=cfn-hup daemon
[Service]
Type=simple
ExecStart=/opt/aws/bin/cfn-hup
Restart=always
[Install]
WantedBy=multi-user.target
mode: "000400"
owner: root
group: root
commands:
01_enable_cfn-hup:
command: "systemctl enable cfn-hup.service"
02_start_cfn-hup:
command: "systemctl start cfn-hup.service"
@mmasko
Copy link
Author

mmasko commented Dec 6, 2019

Hey, I just saw this. Not used to getting replied on my stuff. Thanks for the feedback, I will look into this and make changes. Been a while since I used this one so warrants a review anyways.

Thanks again.

@womanvsmachine
Copy link

womanvsmachine commented Oct 19, 2021

Found your response on StackOverflow and it gave me a great starting point. This is the updated user data for Python3:

  UserData:
    Fn::Base64: !Sub |
      #!/bin/bash
      apt-get update -y
      apt-get install -y python3-pip
      pip3 install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
      ln -s /usr/local/init/ubuntu/cfn-hup /etc/init.d/
      /usr/local/bin/cfn-init -v --stack ${AWS::StackName} --resource EC2 --configsets setup --region ${AWS::Region}
      /usr/local/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource EC2 --region ${AWS::Region}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment