AWS CloudFormation is a service that allows you to define, create, and manage AWS infrastructure as code (IaC). Instead of manually provisioning resources, you can describe your infrastructure in templates written in JSON or YAML, enabling consistent, automated deployments. Infrastructure as Code (IaC) simplifies infrastructure management by treating configurations as code, which can be version-controlled and reused.
In this article, we’ll delve into the concept of IaC, explore AWS CloudFormation, and guide you through a practical example.
How CloudFormation Works CloudFormation relies on three key components:
Template: A JSON or YAML file that specifies the AWS resources to be created.
Stack: A collection of AWS resources defined in a template, deployed, and managed together.
Change Sets: A way to preview changes to a stack before applying updates to ensure the desired outcomes.
Hands-On: Setting Up Jenkins using CloudFormation
For this project, we will break the deployment into three separate YAML files:
Network.yml: Defines the VPC, subnets, and associated networking components.
Jenkins-server.yml: Specifies the EC2 instance and the latest version of Jenkins setup.
Roles.yml: Manages IAM roles and permissions for secure resource access and more of a backup for Jenkins.
To connect to AWS and deploy these templates, we will use Visual Studio Code (VSCode), and authenticate to AWS via Access Key and Secret Key. Ensure you have AWS CLI installed on your PC. You can confirm by running aws --version
on your terminal.
Let’s get started!
network.yml
AWSTemplateFormatVersion: "2010-09-09"
Description: "CloudFormation template to deploy Jenkins with VPC, EC2, and S3."
Resources:
# VPC
MyVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: "10.0.0.0/16"
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: JenkinsVPC
# Internet Gateway
InternetGateway:
Type: AWS::EC2::InternetGateway
VPCGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref MyVPC
# Public Subnet
PublicSubnet:
Type: AWS::EC2::Subnet
Properties:
CidrBlock: "10.0.1.0/24"
MapPublicIpOnLaunch: true
VpcId: !Ref MyVPC
Tags:
- Key: Name
Value: PublicSubnet
# Route Table and Routes
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref MyVPC
... Find Complete script at github.com/chisomjude
Jenkins-server.yml
Resources:
JenkinsInstance:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
KeyName: my-key-pair # Replace with your key pair
ImageId: ami-0c02fb55956c7d316 # Amazon Linux 2 AMI ID
SubnetId: !Ref PublicSubnet
SecurityGroupIds:
- !Ref JenkinsSecurityGroup
UserData:
Fn::Base64: !Sub |
#!/bin/bash
yum update -y
.... Find Complete script at github.com/chisomjude
roles.yml
Resources:
JenkinsInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref JenkinsRole
JenkinsRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
...Find Complete script at github.com/chisomjude
How to run your cloud formation files
Configure AWS User Account:
Ensure you have AWS CLI installed, then you can confirm this by running
aws —version
Run
aws configure
, enter the Secret and Access key, region, and file format in the prompt
Run cloud formation Yaml files:
First, you validate the file using the following command
aws cloudformation validate-template --template-body file://filename.yml
Next, you run the command to create your stacks using each YAML file in this order
VPC
Roles
Jenkins Server, ( pass the network requirement as parameters)
aws cloudformation create-stack --stack-name <stackname> --template-body file://filename.yml --capabilities CAPABILITY_NAMED_IAM
aws cloudformation create-stack \
--stack-name AppStack \
--template-body file://jenkins-server.yml \
--parameters ParameterKey=PublicSubnet,ParameterValue=<Subnet-ID> \
ParameterKey=JenkinsSecurityGroup,ParameterValue=<SecurityGroup-ID> \
--capabilities CAPABILITY_NAMED_IAM
Visualize the Stack and Resources Created
Stack creation
Resources
Check your server on <IP>:8080
Thank you for your time, If this was helpful Give this article a Like or Clap 👏👏👏
Need a complete script Cloudformation Script? Visit my Github Repo - CF-to-Jenkins
Reference:
https://www.jenkins.io/doc/book/installing/linux/
https://docs.aws.amazon.com/corretto/latest/corretto-17-ug/downloads-list.html
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-guide.html