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"
@dogriverrat
Copy link

dogriverrat commented Feb 7, 2019

Minor type on line 40 ... I think that line should be:

/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource EC2 --region ${AWS::Region}

And I think that command isn't doing anything as this is showing up in /var/log/cloud-init-output.log:

Installed /usr/local/lib/python2.7/dist-packages/lockfile-0.12.2-py2.7.egg
Finished processing dependencies for aws-cfn-bootstrap==1.4
/opt/aws/bin/cfn-init -v --stack SecurityMonkey-CFNInit-Test4 --resource EC2 --configsets setup --region us-east-1
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/init.py", line 891, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 115: ordinal not in range(128)
Logged from file util.py, line 476
/opt/aws/bin/cfn-signal -e 0 --stack SecurityMonkey-CFNInit-Test4 --resource EC2 --region us-east-1
ValidationError: Stack arn:aws:cloudformation:us-east-1:AMID:stack/SecurityMonkey-CFNInit-Test4/39607d80-2b25-11e9-b158-0e7f598ae3ee is in CREATE_COMPLETE state and cannot be signaled

Cloud-init v. 18.3-9-g2e62cb8a-0ubuntu118.04.2 running 'modules:final' at Thu, 07 Feb 2019 22:11:45 +0000. Up 24.87 seconds.
2019-02-07 22:14:32,815 - util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [1]
2019-02-07 22:14:32,833 - cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
2019-02-07 22:14:32,833 - util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python3/dist-packages/cloudinit/config/cc_scripts_user.py'>) failed
Cloud-init v. 18.3-9-g2e62cb8a-0ubuntu1
18.04.2 finished at Thu, 07 Feb 2019 22:14:32 +0000. Datasource DataSourceEc2Local. Up 192.03 seconds

Sorry about the formatting above. I'm not a Github poweruser at all :)

@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