Showing posts with label CloudFormation. Show all posts
Showing posts with label CloudFormation. Show all posts

AWS CloudFormation Exercise 6: VPC creation

Exercise 6: Cloud Formation Template for VPC

In This Exercise, we are going to configure VPC using the Cloud Formation Template written in YAML format.

We are going to create a VPC in Mumbai Region (ap-south-1), with 4 subnets spread across two availability zone i.e. ap-south-1a and ap-south-1b. Out of 4, two of them are public and the remaining two are private subnets. Two route tables will be created, one for the public subnets and another for the private subnets.  Internet gateway and Nat gateway will be created and route table entries will be added in the respective route tables.

To get the Stack click on the link: https://github.com/pranavdhopey

and save it to server say under /opt directory.

1.   Login to AWS Management Console. AWS Console

2.   On Management Console click on “Cloud Formation” under the “Management and Governance” section.

3.   Now you will land on Cloud Formation Console. In Cloud Formation Console click on the “Create Stack” button.

Now follow the below steps to create a stack for this exercise

Step 1: Specify template

In this section choose the “Template is Ready” button and select “Upload a template file”. Now choose a file to upload from your personal computer where it is saved and upload. Now click on next.

Step 2: Specify stack details

Now Specify “Stack name” for e.g. Say “TestVPC” for this exercise. Now provide the values for parameters need to create VPC stack, here we are giving the below parameter values.

1. VpcCIDR: 192.168.0.0/16 (Values to be replace)

2. PublicSubnet1CIDR: 192.168.0.0/24 (Values to be replace)

3. PublicSubnet2CIDR: 192.168.1.0/24 (Values to be replace)

4. PrivateSubnet1CIDR: 192.168.11.0/24 (Values to be replace)

5. PrivateSubnet2CIDR: 192.168.12.0/24 (Values to be replace)

6. EnvironmentName: Dev/Test/Prod

Step 3: Configure stack options

On the “Configure stack options” page leave all settings default and click on next.

Step 4: Review Stack

In this step review all the settings that you have filled in and then click create stack.

After some time stack will be created and you can view and access resources created by the cloud formation stack.

Click below to get started

Create Stack

 

We can also create a stack using AWS CLI

AWS CLI for creating stack:

Note: Replace Template file name accordingly.

1.   To validate cloud formation template template

#aws cloudformation validate-template --template-body file:///<path-to-file>/

CFNVPCConfigurationStack.yml

 

2.   To create stack

#aws cloudformation create-stack --stack-name TestVPC --template-body file:///

<path-to-file>/CFNVPCConfigurationStack.yml --parameters file:///<path-to

file>/parameters.json

Here parameters are passed in “parameters.json” file to avoid a mess on the command line. Snapshot is given below.




3.   To describe stack

#aws cloudformation describe-stacks --stack-name TestVPC


4.   To view the stack events

#aws cloudformation describe-stack-events --stack-name TestVPC


5.   To delete the stack

#aws cloudformation delete-stack --stack-name TestVPC


This completes VPC creation using a cloud formation template with various parameters.

 

Deploy CloudFormation stack using Jenkins

In this post, we are going to deploy Cloud Formation Stack using Jenkins FreeStyle project.

Cloud Formation Stack we are going to use is available here: EC2Stack

Download the stack file and save it in your s3 bucket as we are using the s3 bucket URL as a template-body in cloud formation cli.  

Follow the below steps:

Step1: Go to Jenkins Dashboard and click on “New Item”, New screen will appear on that enter the item name say “CFNStackDeploy” and select Freestyle Project and then click on “OK”



Step2: Now under the “General” section select the check box “This project is parameterized” and then add “String Parameter”. Now provide Name, Default Value, And Description, here we have given Name as “StackName”, Value as “ExampleStack” and Description as “Provide Stack Name”

This parameter we are going to invoke in the shell while executing the stack command under the build section.


Step3: Now under the “Build Environment” section, select the check box “Use secret text(s) or file(s)” and add “secret text”. Here provide the Variable names and select Credentials Values as shown in the image. 

Click on this link to know how to save AWS credentials in Jenkins.


Step4: Now under the “Build” section add a build step to “Execute Shell” and copy the below command as given. And Then Click on “Apply” and “Save”

#aws cloudformation create-stack --stack-name ${StackName} --template-body https://<bucket-name>.s3.ap-south-1.amazonaws.com/CFNEC2TemplateBasic.yml --parameters ParameterKey=MyKeyName,ParameterValue=Mumbai ParameterKey=SecurityGroupIds,ParameterValue=sg-xxxxxxxxxx ParameterKey=MyAvailabilityZone,ParameterValue=ap-south-1a ParameterKey=MySubnetId,ParameterValue=subnet-xxxxxxxxxx --region ap-south-1 

Modify the above command as per your configuration. Change s3 bucket URL, change Keyname, security group id, availability zone, subnet id, region to deploy.

Step5: Now go to the project and click on “Build with Parameter”, provide stack name “Anything”, then click on build.


Go to the Console Output to see the build status.


Also, verify CloudFormation dashboard whether the stack is deployed successfully. And verify the instance launched.




This is how we have configured our Freestyle Jenkins Job using Parameters, Secret text credentials, Shell command.


AWS CloudFormation Exercise 5: Network Load Balancer creation

Exercise 5: Cloud Formation Template for Network Load Balancer

In This Exercise, we are going to create Network Load Balancer using the Cloud Formation Template written in YAML format. For this exercise, we need to keep few things ready.
1.   VPC (Default or Custom)
2.   Public Subnets
We are going to create a load balancer in Mumbai Region (ap-south-1), so we have configured VPC with public subnets (already configured in case of the default VPC) and the security group is configured with Port 80 and 443 inbound, we need to select subnets for each Availability Zone so that load balancer would route traffic to those subnets.

To get the Stack click on the link: Without SSL or With SSL and save it to server say under /opt directory.

1.   Login to AWS Management Console. AWS Console

2.   On Management Console click on “Cloud Formation” under the “Management and Governance” section.

3.   Now you will land on Cloud Formation Console. In Cloud Formation Console click on the “Create Stack” button.

Now follow the below steps to create a stack for this exercise

Step 1: Specify template

In this section choose the “Template is Ready” button and select “Upload a template file”. Now choose a file to upload from your personal computer where it is saved and upload. Now click on next.

Step 2: Specify stack details

Now Specify “Stack name” for e.g. Say “NLBStack” for this exercise. Now provide the values for parameters need to create Network Load Balancer stack, here we are giving the below parameter values.

1. VPCId: vpc-xxxxxxxxxx (Values to be replace)

2. MySubnetId: subnet-xxxxxxxxxx, subnet-xxxxxxxxxx, subnet-xxxxxxxxxx (Values to be replace)

Step 3: Configure stack options

On the “Configure stack options” page leave all settings default and click on next.

Step 4: Review Stack

In this step review all the settings that you have filled in and then click create stack.

After some time stack will be created and you can view and access resources created by the cloud formation stack.

Click below to get started

Create Stack

 

We can also create a stack using AWS CLI

AWS CLI for creating stack:

Note: Replace Template file name accordingly.

1.   To validate cloud formation template template

# aws cloudformation validate-template --template-body file:///<path-to-file>/ CFNCreateNLBwithOutput.yml


2.   To create stack

#aws cloudformation create-stack --stack-name NLBStack --template-body

file:///<path-to-file>/CFNCreateNLBwithOutput.yml --parameters

ParameterKey=VPCId,ParameterValue=vpc-xxxxxxxxxx

ParameterKey=MySubnetId,ParameterValue=subnet-xxxxxxxxxx\\,subnet-xxxxxxxxxx\\,subnet-xxxxxxxxxx


3.   To describe stack

#aws cloudformation describe-stacks --stack-name NLBStack

4.   To view the stack events
#aws cloudformation describe-stack-events --stack-name NLBStack

5.   To delete the stack

#aws cloudformation delete-stack --stack-name NLBStack


This completes internet-facing Network LoadBalancer creation using a cloud formation template with various parameters.

AWS CloudFormation Exercise 4: Application Load Balancer creation

 Exercise 4: Cloud Formation Template for Application Load Balancer

In This Exercise, we are going to create Application Load Balancer using the Cloud Formation Template written in YAML format. For this exercise, we need to keep few things ready.
1.   VPC (Default or Custom)
2.   Public Subnets
3.   Security Groups
We are going to create a load balancer in Mumbai Region (ap-south-1), so we have configured VPC with public subnets (already configured in case of the default VPC) and the security group is configured with Port 80 and 443 inbound, we need to select subnets for each Availability Zone so that load balancer would route traffic to those subnets.

To get the Stack click on the link: Without SSL or With SSL and save it to server say under /opt directory.

1.   Login to AWS Management Console. AWS Console

2. On Management Console click on “Cloud Formation” under the “Management and Governance” section.

3. Now you will land on Cloud Formation Console. In Cloud Formation Console click on the “Create Stack” button.

Now follow the below steps to create a stack for this exercise

Step 1: Specify template

In this section choose the “Template is Ready” button and select “Upload a template file”. Now choose a file to upload from your personal computer where it is saved and upload. Now click on next.

Step 2: Specify stack details

Now Specify “Stack name” for e.g. Say “ALBStack” for this exercise. Now provide the values for parameters need to create Application Load Balancer stack, here we are giving the below parameter values.

1. MyELBSecurityGroups: sg-xxxxxxxxxx(Value to be replace)

2. VPCId: vpc-xxxxxxxxxx (Values to be replace)

3. MySubnetId: subnet-xxxxxxxxxx, subnet-xxxxxxxxxx, subnet-xxxxxxxxxx (Values to be replace)

Step 3: Configure stack options

On the “Configure stack options” page leave all settings default and click on next.

Step 4: Review Stack

In this step review all the settings that you have filled in and then click create stack.

After some time stack will be created and you can view and access resources created by the cloud formation stack.

Click below to get started

Create Stack

 

We can also create a stack using AWS CLI

AWS CLI for creating stack:

Note: Replace Template file name accordingly.

1.   To validate cloud formation template template

#aws cloudformation validate-template --template-body file:///<path-to-file>/CFNCreateALBwithOutput.yml


2.   To create stack

#aws cloudformation create-stack --stack-name ALBStack --template-body

file:///<path-to-file>/CFNCreateALBwithOutput.yml --parameters

ParameterKey=MyELBSecurityGroup,ParameterValue=sg-09f534dac79a40ce2

ParameterKey=VPCId,ParameterValue=vpc-xxxxxxxxxx

ParameterKey=MySubnetId,ParameterValue=subnet-03a896945c3e5eb15\\,subnet-0a8957bd1f2621bf1\\,subnet-01e4eb75db170e6f8

 

3.   To describe stack

#aws cloudformation describe-stacks --stack-name ALBStack


4.   To view the stack events

#aws cloudformation describe-stack-events --stack-name ALBStack


5.   To delete the stack

#aws cloudformation delete-stack --stack-name ALBStack

This completes internet facing Application LoadBalancer creation using a cloud formation template with various parameters.

AWS CloudFormation Exercise 3: Classic Load Balancer creation

Exercise 3: Cloud Formation Template for Classic Load Balancer

In This Exercise, we are going to create internet facing Classic Load Balancer using the Cloud Formation Template written in YAML format. For this exercise, we need to keep few things ready.
1.   VPC (Default or Custom)
2.   Public Subnets
3.   Security Group
We are going to create a load balancer in Mumbai Region (ap-south-1), so we have configured VPC with public subnets (already configured in case of the default VPC) and the security group is configured with Port 80 inbound, we need to select subnets for each Availability Zone so that load balancer would route traffic to those subnets.

To get the Stack click on the link:- Without SSL or With SSL and save it to server say under /opt directory. 

1.   Login to AWS Management Console. AWS Console

2.   On Management Console click on “Cloud Formation” under the “Management and Governance” section.

3.   Now you will land on Cloud Formation Console. In Cloud Formation Console click on the “Create Stack” button.

Now follow the below steps to create a stack for this exercise

Step 1: Specify template

In this section choose the “Template is Ready” button and select “Upload a template file”. Now choose a file to upload from your personal computer where it is saved and upload. Now click on next.

Step 2: Specify stack details

Now Specify “Stack name” for e.g. Say “CLBStack” for this exercise. Now provide the values for parameters need to create Classic LoadBalancer stack, here we are giving the below parameter values.

1. MySubnetId: subnet-xxxxxxxxxx, subnet-xxxxxxxxxx, subnet-xxxxxxxxxx (Values to be replace)

2. MyELBSecurityGroups: sg-xxxxxxxxxx(Value to be replace)

Step 3: Configure stack options

On the “Configure stack options” page leave all settings default and click on next.

Step 4: Review Stack

In this step review all the settings that you have filled in and then click create stack.

After some time stack will be created and you can view and access resources created by the cloud formation stack.

Click below to get started

Create Stack

 

We can also create a stack using AWS CLI

AWS CLI for creating stack:

Note: Replace Template file name accordingly.

1.   To validate cloud formation template template

#aws cloudformation validate-template --template-body file:///<path-to-file>/ CFNCreateCLBwithOutput.yml


2.   To create stack

#aws cloudformation create-stack --stack-name CLBStack --template-body

file:///<path-to-file>/CFNCreateCLBwithOutput.yml --parameters

ParameterKey=MyELBSecurityGroup,ParameterValue=sg-xxxxxxxxxx

ParameterKey=MySubnetId,ParameterValue=subnet-xxxxxxxxxx\\,subnet-xxxxxxxxxx\\,subnet-xxxxxxxxxx


3.   To describe stack

#aws cloudformation describe-stacks --stack-name CLBStack


4.   To view the stack events

#aws cloudformation describe-stack-events --stack-name CLBStack


5.   To delete the stack

#aws cloudformation delete-stack --stack-name CLBStack

This completes internet facing Classic LoadBalancer using a cloud formation template with various parameters.

AWS CloudFormation Exercise 2: EC2 Instance with S3 Access

Exercise 2: Cloud Formation Template for EC2 Instance with S3 Read access IAM role

In This Exercise, we are going to create EC2 Instance using the Cloud Formation Template with S3 Read Access IAM role attached written in YAML format. For this exercise, we need to keep few things ready.
1.   VPC (Default or Custom)
2.   Public Subnets
3.   Security Groups
4.   KeyPair
We are going to create EC2 Instance in Mumbai Region (ap-south-1), so we have used image-id “ami-0732b62d310b80e97”, we have already created KeyPair separately to access our instance, the security group is configured with Port 22 inbound, Availability Zone we are using here is ap-south-1a.

To get the Stack click on the link: https://github.com/pranavdhopey and save it to server say under /opt directory.

1.   Login to AWS Management Console. AWS Console

2. On Management Console click on “Cloud Formation” under the “Management and Governance” section.

3. Now you will land on Cloud Formation Console. In Cloud Formation Console click on the “Create Stack” button.

Now follow the below steps to create a stack for this exercise

Step 1: Specify template

In this section choose the “Template is Ready” button and select “Upload a template file”. Now choose a file to upload from your personal computer where it is saved and upload. Now click on next.

Step 2: Specify stack details

Now Specify “Stack name” for e.g. Say “EC2S3AccessStack” for this exercise. Now provide the values for parameters need to create EC2 stack, here we are giving the below parameter values.

1. InstanceName: WebServer(Value to be replace)

2. MyInstanceType: t2.micro(Choose From DropDown)

3. MyKeyName: MumbaiKP(Provide your KeyPair Name)

4. MyAvailabilityZone: ap-south-1a(Value to be replace)

5. MySubnetId: subnet-xxxxxxxxxx(Value to be replace)

6. MySecurityGroups: sg-xxxxxxxxxx(Value to be replace)

Step 3: Configure stack options

On the “Configure stack options” page leave all settings default and click on next.

Step 4: Review Stack

In this step review all the settings that you have filled in and under the capabilities section click on the radio button to acknowledge that AWS CloudFormation might create IAM resources and then click create stack.

After some time stack will be created and you can view and access resources created by the cloud formation stack.

Click below to get started

Create Stack

 

We can also create a stack using AWS CLI

AWS CLI for creating stack:

1.   To validate cloud formation template template

#aws cloudformation validate-template --template-body file://<path-to-file>/CFNEC2LaunchTeplatewithS3Access.yml


2.   To create stack

#aws cloudformation create-stack --stack-name EC2S3AccessStack

--templatebody file://<path-to-file>/CFNEC2LaunchTeplatewithS3Access.yml

--parameters ParameterKey= InstanceName,ParameterValue=S3AccessServer

ParameterKey=MyKeyName,ParameterValue=MumbaiK

ParameterKey=MyInstanceType,ParameterValue=t2.micro

ParameterKey=MyAvailabilityZone,ParameterValue=ap-south-1a

ParameterKey=MySecurityGroups,ParameterValue=sg-xxxxxxxxxx

ParameterKey=MySubnetId,ParameterValue=subnet-xxxxxxxxxx --

capabilities CAPABILITY_IAM


3.   To describe stack

#aws cloudformation describe-stacks --stack-name EC2S3AccessStack


4.   To view the stack events

#aws cloudformation describe-stack-events --stack-name EC2S3AccessStack


5.   To delete the stack

#aws cloudformation delete-stack --stack-name EC2S3AccessStack

This completes EC2 instance creation with S3 Read Access IAM role attached using a cloud formation template with various parameters.

AWS CloudFormation Exercise 1: EC2 Instance creation

Exercise 1: Cloud Formation Template for EC2 Instance with Apache UserData

In This Exercise, we are going to create EC2 Instance using the Cloud Formation Template written in YAML format. For this exercise, we need to keep few things ready.
1.   VPC (Default or Custom)
2.   Public Subnets
3.   Security Groups
4.   KeyPair
We are going to create EC2 Instance in Mumbai Region (ap-south-1), so we have used image-id “ami-0732b62d310b80e97”, we have already created KeyPair separately to access our instance, the security group is configured with Port 22 and 80 inbound, the subnet we are using here is ap-south-1a.

To get the Stack click on the link: https://github.com/pranavdhopey and save it to server say under /opt directory.

1.   Login to AWS Management Console. AWS Console

2.   On Management Console click on “Cloud Formation” under the “Management and Governance” section. 

3.   Now you will land on Cloud Formation Console. In Cloud Formation Console
click on the “Create Stack” button.

Now follow the below steps to create a stack for this exercise.

Step 1: Specify template

In this section choose the “Template is Ready” button and select “Upload a template file”. Now choose a file to upload from your personal computer where it is saved and upload. Now click on next.

Step 2: Specify stack details

Now Specify “Stack name” for e.g. Say “EC2Stack” for this exercise. Now provide the values for parameters need to create EC2 stack, here we are giving the below parameter values.

1. MyInstanceType: t2.micro(Choose From DropDown)
2. MyKeyName: MumbaiKP(Provide your KeyPair Name)
3. MyAvailabilityZone: ap-south-1a(Value to be replace)
4. MySubnetId: subnet-xxxxxxxxxx(Value to be replace)
6. MySecurityGroups: sg-xxxxxxxxxx(Value to be replace)

Step 3: Configure stack options

On the “Configure stack options” page leave all settings default and click on next.

Step 4: Review Stack

In this step review all the settings that you have filled in and click on create stack.

After some time stack will be created and you can view and access resources created by the cloud formation stack.

Click below to get started:

Create Stack

We can also create a stack using AWS CLI.

AWS CLI for creating stack:

1.   To validate cloudformation template template

#aws cloudformation validate-template --template-body file://<path-to-file>/CFNTemplatesWithApacheUserData.yml


2.   To create stack

#aws cloudformation create-stack --stack-name EC2Stack1 --template-body

file://<path-to-file>/CFNTemplatesWithApacheUserData.yml --parameters

ParameterKey=MyKeyName,ParameterValue=MumbaiKP

ParameterKey=MyInstanceType,ParameterValue=t2.micro

ParameterKey=MyAvailabilityZone,ParameterValue=ap-south-1a

ParameterKey=MySecurityGroups,ParameterValue=sg-xxxxxxxxxx

ParameterKey=MySubnetId,ParameterValue=subnet-xxxxxxxxxx

3.   To describe stack
#aws cloudformation describe-stacks --stack-name EC2Stack

4.   To view the stack events
#aws cloudformation describe-stack-events --stack-name EC2Stack


5.   To delete the stack

#aws cloudformation delete-stack --stack-name EC2Stack

This completes EC2 instance creation using a cloud formation stack with various parameters.