devops with aws
Project setup
Importance of automated test in CI,CD
- Automated tests
- Unit tests
- Integration tests
- UAT tests
- Code coverage
- Notifications
CI/CD with relational databases
- Managing the version of database schema
There is no easy way to control the version of relational database schema - Database schema migrations
- DellStore2 sample database
- Products table
- Sqitch change management system
Project component setup
- PostgreSQL database on AWS RDS
- Node.JS HAPI RESTful API project
- Sqitch database mangement framework
Setup PostreSQL database instance in AWS RDS
- Create a rds in aws with postgresql engine version 9.4.7.
- Connect to aws rds use pgAdmin 3.
- Download sample schema dellstore2 from link
- In pgAdmin3 click Plugins-> PSQL console-> run command
\i /tmp/dellstore2.sql
to create a new schema.Setup Node.JS HAPI ReSTful API project
HAPI is a rich application framework for building applications and RESTful APIs with Node.JS
Official website for HAPI framework is HAPIJS.com
install node and npm
1
2
3
4
5
6node -v
npm -v
mkdir myfirsthapiproject
cd myfirsthapiproject
npm init
npm install --save hapi1
2
3
4
5git clone https://github.com/espiderinc/hapi-rest-demo.git
cd hapi-rest-demo
npm install
sudo npm install -g istanbul mocha
node index.js)
test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27hapi-rest-demo git:(master) ✗ npm test
> hapi-rest-demo@1.0.0 test /home/stan/workspace/hapi-rest-demo
> istanbul cover _mocha test/**/*.js
(node:20777) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
Task routes
GET /products
✓ should return statusCode 200 (381ms)
✓ should return product [ACADEMY BROOKLYN]
2 passing (416ms)
=============================================================================
Writing coverage object [/home/stan/workspace/hapi-rest-demo/coverage/coverage.json]
Writing coverage reports at [/home/stan/workspace/hapi-rest-demo/coverage]
=============================================================================
=============================== Coverage summary ===============================
Statements : 56.31% ( 58/103 )
Branches : 39.29% ( 11/28 )
Functions : 47.83% ( 11/23 )
Lines : 57% ( 57/100 )
================================================================================report generated workspace/hapi-rest-demo/coverage/lcov-report/index.html
Setup swtich (database schema framework)
Managin database schema for relational databaes (with Sqitch)
Sqitch is a standalone system without any dependency on frameworks or ORMs.
- handels dependencies between scripts
- project site
install sqitch
1 | docker pull sqitch/sqitch |
use sqitch
1 | mkdir stantutorial |
CI and CD pipeline deep dive
AWS prerequisites
- IAM instance profile
- Create a policy, name: CodeDeploy-EC2-Permissions, json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15{
"Version": "2012-10-17",
"Statement": [
{
"Action":[
"s3:Get*",
"s3:List*"
],
"Effect": "Allow",
"Resource": "*"
}
]
} - Create a role, named CodeDeploy-EC2 -> Choose role type ec2->Attach permissions policies “CodeDeploy-EC2-Permissions”
- IAM service role
- Create a role named stantutorialRole -> select role type CodeDeploy
Jenkins installation
Ubuntu-> Configure Instance Details, IAM role, select CodeDeploy-EC2 (this will allow jenkins connect to s3 buckets)->
Tag instance: Key group Value hapi-demo
1 | wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key| sudo apt-key add - |
Install plugin AWS CodePipeline
Install node.js:
1 | curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - |
Install sqitch
1 | sudo apt-get install build-essential cpanminus perl perl-doc |
Create a new instance hapi-demo install node.js and
1 | sudo apt install python3-pip |
CodeDeploy application
Create a new Codedeploy application, choose compute type ec2/on-premises, Service role-> statutorialRole; Environment configuration tick Amazon EC2 instances, Key-> Name, Value-> hapi-demo; Deployment setting-> CodeDeployDefault.OneAtATime
Review appSpec.yml file
appspec.yml file is an application specification file for aws codedeploy
1 | cat appspec.yml |
Setup Jenkins job
Create a freestyle jenkins job, configurat as following screenshots:
Build AWS CodePieline
- Source provider GitHub
- Build provider: Add Jenkins; Prvider name must match the name in jenkin’s job
- Deployment provider: aws codedeploy
Next Steps
Notifications
AWS SNS notifications for build and deployment status
- Create a policy named: notification-policyAttach notification-policy to Role CodeDeploy-EC2
1
2
3
4
5
6
7
8
9
10{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sns:Publish",
"Resource": "*"
}
]
} - in CodeDeploy edit deployment group;
Code changes
Automatically and continuously deploy code without any downtime
Database schema changes
Consistently and automatically deploy relational database schema changes
1 | sqitch add product-add-comments |