Skip to content

Commit

Permalink
Added functions, modified Jenkinsfile (copy code to s3 bucket)
Browse files Browse the repository at this point in the history
  • Loading branch information
mslosarz committed Mar 8, 2019
1 parent 2d15305 commit 8697149
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 42 deletions.
38 changes: 0 additions & 38 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,46 +1,8 @@
# package directories
node_modules

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### Swagger code gen ###
.swagger-codegen
# Logs
logs
*.log
npm-debug.log*

# Dependency directories
node_modules/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# nyc test coverage
coverage
.nyc_output

# JetBrains IDEs
.idea

# Git merge tool
*.orig

# Build dir
dist
12 changes: 8 additions & 4 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ pipeline {
}
}
}
stage('CloudFormation setup') {

stage('Setup s3 deployment bucket') {
steps {
script {
functions.validateTemplate()
Expand All @@ -42,12 +43,15 @@ pipeline {
}
}
}
stage('Test') {

stage('Copy Lambda code to S3 deployment bucket') {
steps {
echo 'Testing..'
script {
functions.uploadLambdaCode()
}
}
}
stage('Deploy') {
stage('Test lambda') {
steps {
echo 'Deploying....'
}
Expand Down
35 changes: 35 additions & 0 deletions build/functions.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Environment name will be taken from the ROOT folder
def env(){
return env.JOB_NAME.replace("/${env.JOB_BASE_NAME}", '')
}

def validateTemplate() {
def templates = findFiles(glob: 'cfn/**')
for (template in templates) {
cfnValidate(file: template.getPath())
}
}

def updateDeploymentBucket() {
def envName = env()
cfnUpdate(stack: envName+'-deployment-bucket', file: 'cfn/deployment/s3bucket.cfn.yaml', params: ['BucketName': envName], timeoutInMinutes: 10, tags: ['Environment='+envName], pollInterval: 10000)
}

def getDeploymentBucketName() {
def envName = env()
return cfnDescribe(stack: envName+'-deployment-bucket').BucketName
}

def getDeploymentPath() {
return "artifacts/${env()}/${env.JOB_BASE_NAME}/${env.BUILD_NUMBER}/cfn"
}

def uploadTemplates() {
s3Upload(file: 'cfn', bucket: getDeploymentBucketName(), path: getDeploymentPath())
}

def uploadLambdaCode() {
s3Upload(file: 'dist/lambda.zip', bucket: getDeploymentBucketName(), path: getDeploymentPath())
}

return this
53 changes: 53 additions & 0 deletions cfn/lambda/lambda.cfn.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
AWSTemplateFormatVersion: '2010-09-09'
Description: Template for typescript lambda
Parameters:
Environment:
Type: String
Description: Environment name
DeploymentBucket:
Type: String
Description: Deployment bucket name
LambdaZipPath:
Type: String
Description: Path to zip file with lambda code (including build number etc)

Resources:
TypescriptLambda:
Type: AWS::Lambda::Function
DependsOn:
- LambdaLogGroup
Properties:
FunctionName: !Sub '${Environment}-${AWS::Region}-typescript-lambda'
Description: Sample lambda written in typescript
Runtime: nodejs8.10
Code:
S3Bucket: !Ref DeploymentBucket
S3Key: !Ref LambdaZipPath
Handler: dist/handler.handler
MemorySize: 128
Role: !Ref LambdaExecutionRole
Timeout: 10

LambdaLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub '/aws/lambda/${TypescriptLambda}'
RetentionInDays: 3

LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

Outputs:
TypescriptLambda:
Value: !Ref TypescriptLambda

0 comments on commit 8697149

Please # to comment.