Infrastructure as code Automation
In this article i would like to focus on the most value part of DevOps which is Automation.
In this category we will learn how it works and look at a simple example of Infra as code and it’s automation.
Imagine the situation in which you start rolling on a new application, you configure your Local environment, Development Stage, Test Stage , QA Stage , maybe PreProduction Stage and finally Production. Wow , seems a Huge work, your application code will be shared on all theses stages , but the Ips will change, the Servers will change, the infrastructure will change based on the importance of the stage and it’s proper environment, do you like to configure on thing multiple times? do you like have more error pron tasks? , surely no, and surely you not gonna lost the money to duplicate the production power full resources on your test environment, or inversely lost the performance and SLA by duplicating the Test environment configuration on production.
Here we ll learn how to do the first step Automating IaC on AWS :
- Convert your infrastructure to a document base source
- Parameterize your source
- Implement a source versioning strategy
- Automate commitment and change detections
- Run Continuous Delivery
What we love to have followings
- Run Continuous integration
- Run Continuous Deployment
What we must have
- Automating Tests
Amazon web services Infra As Code:
AWS introduces CloudFormation to simplify the DevOps word, by allowing you to have human readable , versioned and parameterized infrastructure resources.
Human Readable : You can write them easily in JSON or Yaml formats. camparing to the XML based structures and Modern Coding Languages as C# , Java is easy to understand and you don’t need to have any competency on Coding
Versioned: When i say versioned i don’t face to talk about Git, GitHub, SVN, CVS or TFS, I just want to say the Cloud Formation Can recognize your template modifications and execute just your modification , it is really intelligent to find your stack , compare the new version of template with the last successful released one.
Parameterized : Each Cloud formation Template consists of some structured parts which help you to make the rich templates, fully dynamic and extensible.
A template has 1 mandatory part:
- Resource
And has useful parts as:
- Parameters
- Conditions
- Metadata
- Mappings
To Know More About CFT’s :
Example :
At the first step we create a simple template
AWSTemplateFormatVersion: 2010-09-09
Resources:
MyLambda:
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: myLambdaToTestIaC
Code:
S3Bucket: myiacsourcebucket
S3Key: lambdacode.zip
Handler: >-
index.handler
Role: arn:aws:iam::<account_id>:role/lambda_basic_execution
Runtime: nodejs8.10
Timeout: 15
This template creates a new lambda function in aws plateform, why i choose it as this creation will be free in your environment for a learning purpose.
Now , we need to build our CI/CD process , by using CodePipeline we construct this important step but before wee need to have our first part.
Lets do theses steps:
- Upload template to S3
- Launch Cloud formation Stack
- Implement the source of Pipeline
- Implement the Deploy step
S3 Bucket:
- In Aws Console create a new bucket called myiaccourcebucket
- Upload the template you have already saved on local drive
Ajouter une description
Click next , accept all by default and continue bucket creation steps
Now, Upload zipped template to your bucket
Ajouter une description
You need to zip the template and upload it on a standard s3 with all default config, just do next and finally upload.
You need to give cloud formation to find the template on your bucket, by default the buckets are not public , for learning purpose we make the bucket public.
You Need to Enable Versioning on the bucket.
CloudFormation : You need to launch the CloudFormation Stack for the first time.
Go to Cloud Formation and select launch stack as bellow
Ajouter une description
Launch stack by Selecting the yaml template from your local or upload it to S3 bucket
Ajouter une description
In the Next screen you will be asked for the stackname ,
Ajouter une description
For the rest accept all defaults and go ahead , once the stack created go to CodePipeline service.
CodePipeline:
Codepipeline helps us to Build , Test and deploy our sources in a structured manner, for example you can get your java or C# code from GitHub and build it and finally deliver it to your QA environment or production. you can automate the deployment .
Create Pipeline : by creating the pipeline you can implement the CI/CD process using CodeCommit , CodeBuild , CodeDeploy but in a connected and managed fashion.
let’s start it by selecting the create pipeline button, in the first step we configure our source to create the artifact .
Ajouter une description
Type a name and select a role and the artifact store default, you can select a custom location for artifact , but at this point use the default on , and consider that the default artifact source name will be as SourceArtifact. we need to use it later
Now, Configure the source stage , here we configure how and where discover our source
Ajouter une description
The change detection can be done by CodePipeline or by CloudWatch events
In the next screen skip the build step, we don’t need a build on CloudFormation templates.
Configure Deploy Screen as bellow ,
Ajouter une description
The Stack Name will be the name of stack you have already created before , and the template consists of the source artifact and the template file in the artifact, for the role use a role customized or a role with admin access.
Select Next and click on create pipeline then. within some seconds the pipeline will be started. the source and deploy stage will run in sequence.
The stack status will be changed as UPDATE_COMPLTE.
Let’s test the cloudformation versioning by modifing the name of lambda in the template and re-uploading the zip file. as soon as you upload the file the codepipeline runs the process again as bellow , and your lambda name will be changed.
Ajouter une description
In the next article we will find how to test our templates before deploying and parameters in the templates.