Start cfn-init in Ubuntu instance with cloudformation (yaml)
Asked Answered
S

6

6

I try to start the cfn-init with:

Fn::Base64: !Sub | 
  #!/bin/bash
  sudo apt-get -y install python-setuptools
  mkdir aws-cfn-bootstrap-latest
  curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz | tar xz -C aws-cfn-bootstrap-latest --strip-components 1
  sudo easy_install aws-cfn-bootstrap-latest
  sudo /usr/local/bin/cfn-init --stack !Ref 'AWS::StackName' --resource xxx --region !Ref 'AWS::Region'

The first steps work. I can access the instance and cfn-init is installed. When I execute the cfn-init --stack.. command inside my ec2 instance it works fine when I hardcode the values for stackname and region.

How to make it work in the yaml script? It seems it can not read the values for StackName and region.

Subscription answered 11/4, 2018 at 9:41 Comment(0)
K
4

If you are using !Sub you need to wrap your variables with ${} instead of using !Ref

Try this

Fn::Base64: !Sub | 
  #!/bin/bash
  sudo apt-get -y install python-setuptools
  mkdir aws-cfn-bootstrap-latest
  curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz | tar xz -C aws-cfn-bootstrap-latest --strip-components 1
  sudo easy_install aws-cfn-bootstrap-latest
  sudo /usr/local/bin/cfn-init --stack ${AWS::StackName} --resource xxx --region ${AWS::Region}

If you specify template parameter names or resource logical IDs, such as ${InstanceTypeParameter}, AWS CloudFormation returns the same values as if you used the Ref intrinsic function. If you specify resource attributes, such as ${MyInstance.PublicIp}, AWS CloudFormation returns the same values as if you used the Fn::GetAtt intrinsic function.

For more details check AWS - Fn::Sub

Kitchen answered 11/4, 2018 at 9:45 Comment(1)
I tried with ubuntu. didn'\t work. any solution?Selfseeking
F
4

This worked for me:

        UserData:
          Fn::Base64: !Sub |
            #!/bin/bash

            sudo apt-get install -y build-essential python3-pip
            sudo pip3 install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
Filmer answered 23/6, 2022 at 17:10 Comment(0)
K
1

Amazon published a few different example scripts to do this on various different operating systems.

Original question about installing helper scripts on Ubuntu: https://repost.aws/knowledge-center/install-cloudformation-scripts

Direct link to Ubuntu templates: https://github.com/awslabs/aws-cloudformation-templates/tree/master/aws/solutions/OperatingSystems

Keyser answered 10/4, 2023 at 19:39 Comment(0)
S
0
Fn::Base64: !Sub | 
  #!/bin/bash
  sudo apt-get -y install python-setuptools
  mkdir aws-cfn-bootstrap-latest
  curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz | tar xz -C aws-cfn-bootstrap-latest --strip-components 1
  sudo easy_install aws-cfn-bootstrap-latest
  sudo /usr/local/bin/cfn-init --stack ${AWS::StackName} --resource xxx --region ${AWS::Region}

IF easy_install didn't work for ubuntu 18.04 then use this following command:

python /usr/lib/python2.7/dist-packages/easy_install.py aws-cfn-bootstrap-latest
Shock answered 15/2, 2020 at 19:21 Comment(0)
S
0

If the easy_install command not found then try this it works for me

"UserData": {
  "Fn::Base64": { "Fn::Join":["", [
    "#!/bin/bash\n",
    "apt-get update\n",
    "apt-get install -y python-setuptools\n",
    "mkdir -p /opt/aws/bin\n",
    "apt-get install -y wget\n",
    "wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz\n",
    "python3 -m easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-py3-latest.tar.gz\n",
    "/usr/local/bin/cfn-init --stack ", { "Ref":"AWS::StackName" }, " --resource WebServer", " --region ", { "Ref": "AWS::Region" }, "\n",
    "\n",
    "/usr/local/bin/cfn-signal --exit-code $? '", { "Ref" : "WaitHandle" }, "'\n"
  ]]}
}
Strath answered 6/5, 2022 at 14:5 Comment(0)
T
0

The previous solutions will not work for Ubuntu 24.04 LTS which uses python3.12. Instead, the package aws-cfn-bootstrap (containing cfn-init etc.) can be installed using a virtual environment for python (version 11 or lower).

An example solution for the UserData section in the Cloudformation template could be as follows:

# python 3.11 virtualenv installation
add-apt-repository -y ppa:deadsnakes/ppa
apt install -y python3.11 python3-pip

# virtualenv set-up (pip binaries will use PATH=/opt/aws/virtualvenv/bin:...)
python3.11 -m pip install virtualenv
virtualenv /opt/aws/virtualvenv
source /opt/aws/virtualvenv/bin/activate

# install aws-cfn-bootstrap dependencies
pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz

# testing (no need to specify the path)
cfn-init -v --stack ${AWS::StackId} --resource EC2Instance --configsets full_install --region ${AWS::Region}
cfn-signal -e $? --stack ${AWS::StackId} --resource EC2Instance --region ${AWS::Region}
Trimorphism answered 26/9, 2024 at 6:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.