Jenkins submit CloudFormation template to AWS

In this article, we will configure Jenkins to use AWS via slave nodes to deploy some CloudFormation template. Before starting to work you need to create some AWS keys with programmatic access and one key pair to connect to the Linux machine. If you want to configure Jenkins with the slave nodes please use this link.

To create ‘Access Key ID‘ and ‘Secret Access Key‘ login to AWS console and then in the IAM service create a new user with Programmatic access. Then copy to some place this keys:
aws-key-create

Select Administrator Access:
Select-Administrator-Access.png

Then press ‘Create user‘ buton.
Create-User

At the end copy ‘Access key ID‘ and ‘Secret Access key‘ to keep in some secure place. In the next steps we will use them.
copy-access-secre-keys

We need to create key pair which will be used to connect to the EC2 linux instanse. In the AWS console go to the ‘EC2‘ -> In the left side select ‘Key Pairs‘ -> Press ‘Create Key Pair‘ button and Enter name and press button to create
create-key-pair

Then in the Jenkins console we need to configure new credentials for AWS. In the main console go to the Credentials -> Press (global) button
Jenkins-new-Credentials

Then press Add Credentials button to add new credentials:
global-credentials

Add AWS ‘Access Key ID‘ which created before and press ‘OK‘ button:
aws-access-key

Add AWS ‘Secret Access Key‘ which created before and press ‘OK‘ button:
aws-secret-key

Create new Project. In the main page press New Item :
Jenkins-New-Project.png

Select Freestyle project -> Enter some name (in my case: DeployCloudFormationTemplate) -> Press OK button
Jenkins-New-Project-Name

In the opened page click in the check box ‘Restrict where this project can be run‘ and input ‘swarm‘ (It is group of slave servers) in the ‘Label Expression‘ then press ‘Apply‘button.
select-slave-nodes

Then go to down in the ‘Build‘ section press ‘Add build step‘ and select ‘Execute shell‘ :
build-step-execute-shell

In the opened command place enter the following command and press ‘Apply‘ button. This command will download template file with parameters which will select instance type, KeyPair name and our Public IP to give as parameter to open in the Firewall(it is for SSH access):

$ aws cloudformation create-stack --stack-name JenkinsToAWSDeploy --template-url https://s3-us-west-2.amazonaws.com/cloudformation-templates-us-west-2/EIP_With_Association.template --parameters ParameterKey=InstanceType,ParameterValue=t2.micro ParameterKey=KeyName,ParameterValue=fromJenkinstoAWS ParameterKey=SSHLocation,ParameterValue=$(curl -s ifconfig.io)/32

build-shell-command

In the ‘Build Environment‘ section select ‘Use secret text(s) or file(s)
build-environment

In the opened ‘Bindings‘ section press ‘Add‘ and ‘Secret text‘ button (This step must be repeated for ‘Access Key ID‘, ‘Secret Access Key‘)
build-environment-bindings

Add new variables for ‘Access Key ID‘ and ‘Secret Access Key‘ and press ‘Apply‘ button:
access-and-secret-keys

Go to the up in the ‘General‘ tab and select checkbox ‘This project is parameterized‘ -> Press ‘Add Parameter‘ -> ‘String Parameter
add-string-parameter
string-parameter

Input parameter name, value and description and then press ‘Apply‘ and ‘Save‘ button:
string-parameter-name

Press ‘Build with Parameters‘ -> then ‘Build‘ button
build-with-parameters
build-button

Press little icon in down at the left side to see the log output:
build-loading

Look at the result:
build-output

Look to the CloudFormation status in the AWS side:
cloudformation-status

Go to the EC2 instance and take the name of linux instance. We will use this name to connect to out Linux machine.
ec2-instance-domain-name

From GitBash console try to login to the Linux instance with key which created before:

$ ssh -i fromJenkinstoAWS.pem ec2-user@ec2-52-16-191-40.eu-west-1.compute.amazonaws.com

ssh-output

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s