diff --git a/.gitignore b/.gitignore index a7e589e..63694ad 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Jenkinsfile b/Jenkinsfile index 0a8072c..8947f93 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -33,7 +33,8 @@ pipeline { } } } - stage('CloudFormation setup') { + + stage('Setup s3 deployment bucket') { steps { script { functions.validateTemplate() @@ -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....' } diff --git a/build/functions.groovy b/build/functions.groovy new file mode 100644 index 0000000..c640824 --- /dev/null +++ b/build/functions.groovy @@ -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 diff --git a/cfn/lambda/lambda.cfn.yaml b/cfn/lambda/lambda.cfn.yaml new file mode 100644 index 0000000..ae82a69 --- /dev/null +++ b/cfn/lambda/lambda.cfn.yaml @@ -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