Skip to content

Commit 65cdaf3

Browse files
committed
create repo
0 parents  commit 65cdaf3

File tree

16 files changed

+743
-0
lines changed

16 files changed

+743
-0
lines changed

.eslintrc.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": ["eslint:recommended"],
3+
"parserOptions": {
4+
"ecmaVersion": 2018,
5+
"sourceType": "module"
6+
},
7+
"env": {
8+
"es6": true,
9+
"node": true
10+
}
11+
}

.prettierrc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
singleQuote: true
2+
printWidth: 120

app/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# package directories
2+
node_modules
3+
jspm_packages
4+
5+
# Serverless directories
6+
.serverless
7+
8+
# ignornig this beause account details are written for CodeArtifact
9+
package-lock.json

app/handler.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports.hello = async event => {
2+
return {
3+
statusCode: 200,
4+
body: JSON.stringify(
5+
{
6+
message: 'Your function executed successfully',
7+
input: event,
8+
},
9+
),
10+
};
11+
};

app/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "app",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "handler.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"devDependencies": {
13+
"serverless": "^2.7.0"
14+
}
15+
}

app/serverless.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
service: myapp
2+
3+
provider:
4+
name: aws
5+
runtime: nodejs12.x
6+
stage: ${opt:stage}
7+
region: us-west-2
8+
9+
functions:
10+
hello:
11+
handler: handler.hello

buildspec.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
version: 0.2
2+
3+
phases:
4+
install:
5+
runtime-versions:
6+
nodejs: 12
7+
pre_build:
8+
commands:
9+
- yum install epel-release -y
10+
- yum install jq -y
11+
- param_name=/myapp/$STAGE/deploy_role
12+
- DEPLOY_ROLE=$(aws ssm get-parameter --name $param_name | jq ".Parameter.Value" | tr -d \")
13+
- aws codeartifact login --tool npm --repository artifacts-repo --domain artifacts-domain --domain-owner $ACCOUNT_ID
14+
- npm -d ping
15+
build:
16+
commands:
17+
- role=$(aws sts assume-role --role-arn $DEPLOY_ROLE --role-session-name deployer-session --duration-seconds $DURATION)
18+
- KEY=$(echo $role | jq ".Credentials.AccessKeyId" --raw-output)
19+
- SECRET=$(echo $role | jq ".Credentials.SecretAccessKey" --raw-output)
20+
- TOKEN=$(echo $role | jq ".Credentials.SessionToken" --raw-output)
21+
- export AWS_ACCESS_KEY_ID=$KEY
22+
- export AWS_SECRET_ACCESS_KEY=$SECRET
23+
- export AWS_SESSION_TOKEN=$TOKEN
24+
- export AWS_DEFAULT_REGION=$AWS_REGION
25+
- cd app && npm i
26+
- $(npm bin)/sls deploy --stage $STAGE -v
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
AWSTemplateFormatVersion: "2010-09-09"
2+
Description: Used as cross-account role by CodeBuild.
3+
Parameters:
4+
CIAccountId:
5+
Type: String
6+
Description: Enter CI account ID
7+
Resources:
8+
DeployerRole:
9+
Type: AWS::IAM::Role
10+
Properties:
11+
Path: /
12+
RoleName: !Sub ${AWS::StackName}
13+
AssumeRolePolicyDocument:
14+
Version: "2012-10-17"
15+
Statement:
16+
- Effect: Allow
17+
Principal:
18+
Service:
19+
- lambda.amazonaws.com
20+
- codepipeline.amazonaws.com
21+
- codebuild.amazonaws.com
22+
- cloudformation.amazonaws.com
23+
AWS: !Sub "arn:aws:iam::${CIAccountId}:role/pipeline-codebuild-role"
24+
Action: sts:AssumeRole
25+
Policies:
26+
- PolicyName: !Sub ${AWS::StackName}-policy
27+
PolicyDocument:
28+
Version: "2012-10-17"
29+
Statement:
30+
- Effect: Allow
31+
Action:
32+
- iam:*
33+
Resource:
34+
- "*"
35+
- Effect: Allow
36+
Action:
37+
- sns:*
38+
Resource:
39+
- "*"
40+
- Effect: Allow
41+
Action:
42+
- cloudformation:*
43+
Resource:
44+
- "*"
45+
- Effect: Allow
46+
Action:
47+
- lambda:*
48+
Resource:
49+
- "*"
50+
- Effect: Allow
51+
Action:
52+
- logs:*
53+
Resource:
54+
- "*"
55+
- Effect: Allow
56+
Action:
57+
- s3:*
58+
Resource:
59+
- "*"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# dependencies
2+
/node_modules
3+
4+
# serverless build dirs
5+
.serverless
6+
7+
# ignornig this beause account details are written for CodeArtifact
8+
package-lock.json
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const superagent = require('superagent');
2+
const middy = require('middy');
3+
const { ssm } = require('middy/middlewares');
4+
5+
const notify = async (event, context) => {
6+
const { slackUrl } = context;
7+
let data = getMessage(event);
8+
9+
try {
10+
await superagent.post(slackUrl).send(data);
11+
return `Sent to Slack`;
12+
} catch (error) {
13+
return { statusCode: 500 };
14+
}
15+
};
16+
17+
const getMessage = (event) => {
18+
const status = event.detail['build-status'];
19+
const project = event.detail['project-name'];
20+
let account = getJobAccount(event);
21+
const deepLink = event.detail['additional-information'].logs['deep-link'];
22+
23+
let msg = {
24+
attachments: [
25+
{
26+
fallback: `${project} ${status} - ${account}`,
27+
pretext: ``,
28+
color: status === 'SUCCEEDED' ? '#00A542' : '#D00000',
29+
fields: [
30+
{
31+
value:
32+
status === 'SUCCEEDED'
33+
? `Successful deploy to ${account} - CodeBuild Log: ${deepLink}`
34+
: `Failed deploy to ${account} - CodeBuild Log: ${deepLink}`,
35+
short: false,
36+
},
37+
],
38+
},
39+
],
40+
};
41+
return msg;
42+
};
43+
44+
const getJobAccount = (event) => {
45+
let jobAccount = 'No account found';
46+
const envVars = event.detail['additional-information'].environment['environment-variables'];
47+
if (envVars.some((stage) => stage['value'] === 'dev')) {
48+
jobAccount = 'DEV';
49+
} else if (envVars.some((stage) => stage['value'] === 'test')) {
50+
jobAccount = 'TEST';
51+
} else if (envVars.some((stage) => stage['value'] === 'prod')) {
52+
jobAccount = 'PRODUCTION';
53+
}
54+
return jobAccount;
55+
};
56+
57+
module.exports.notify = middy(notify).use(
58+
ssm({
59+
cache: true,
60+
cacheExpiryInMillis: 5 * 60 * 1000,
61+
setToContext: true,
62+
names: {
63+
slackUrl: `/myapp/${process.env.STAGE}/slack_url`,
64+
},
65+
})
66+
);

0 commit comments

Comments
 (0)