=================
sudo rm -rf /var/lib/cloud/* sudo cloud-init init sudo cloud-init modules -m final
========
pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
cfn-init -s StackName -r resourceName --configsets config_set --region region
Sometime cfn-init
may give InsecurePlatformWarning
, so for that we have to install urllib3[secure]
, but do remember to Upgrade it. It may also occur that at first time it will install with error having problem in cryptography
library, but second time it will install, but remember this is not OK, you will again face errors as cryptography
package not installed successfully. To fix that run following command
sudo apt-get install -y build-essential libssl-dev libffi-dev python-dev openssl
sudo pip install -U cryptography
By default cfn-init not installed in ubuntu amis, so how to run cloudformation init script on Ubuntu. Please follow below guide for that.
I am describing CFT in yaml file for simplicity, you can convert it to JSON by any tool. Below is the example in which I write a file chef-configuration file though cfn-init and then run chef client though that configuration file. Please replace Ref
according to your CFT.
Instance:
Type: "AWS::EC2::Instance"
DependsOn:
- "InstanceRolePolicy"
Metadata:
AWS::CloudFormation::Init:
configSets:
full_install:
- "setup1"
setup1:
files:
/tmp/config.json:
content:
Fn::Join:
- ""
-
- '{"override_attributes":{"jenkins-config":{"mmc":{"env":"PROD","jdbc":{"url":"jdbc:mysql://'
- Ref: "MySQLDatabase"
- ':'
- "3306"
- '/","dbname":"'
- Ref: "DBName"
- '","user":"'
- Ref: "DBUser"
- '","pass":"'
- Ref: "DBPassword"
- '"}},'
- '"nfs":{"server":"'
- Ref: "PrivateIp"
- '"}'
- '}}}'
mode: "000600"
owner: "root"
group: "root"
Properties:
ImageId: "ami-f38931e4"
KeyName:
Ref: "KeyPairName"
InstanceType: "t2.micro"
IamInstanceProfile:
Ref: "InstanceRolePolicy"
SecurityGroups:
-
Ref: "EC2SecurityGroup"
UserData:
Fn::Base64:
Fn::Join:
- ""
-
- "#!/bin/bash -xe\n"
- "# Install chef client\n"
- 'curl -LO https://omnitruck.chef.io/install.sh && sudo bash ./install.sh -v 12.12.13-1 && rm install.sh; '
- "sudo apt-get install -y awscli build-essential libssl-dev libffi-dev python-dev openssl\n"
- "aws s3 cp s3://"
- Ref: 'S3Bucketchef'
- '/'
- Ref: 'S3BucketchefKey'
- ' /tmp/chef-repo.tar --region us-east-1; '
- "mkdir -p /tmp/chef-repo\n"
- 'tar -xvf /tmp/chef-repo.tar -C /tmp/chef-repo ; '
- "apt-get -y install python-pip\n"
- "pip install -U urllib3[secure]\n"
- "pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n"
- "/usr/local/bin/cfn-init"
- " --stack "
- Ref: "AWS::StackName"
- " --resource Instance"
- " --configsets full_install"
- " --region "
- Ref: "AWS::Region"
- "\n"
- 'chef-client -c /tmp/chef-repo/client.rb -N crm -j /tmp/config.json'