From 2799b02d6a72d1866e0f8ffc96239a62984a7ce2 Mon Sep 17 00:00:00 2001 From: Michael Wunderlich Date: Thu, 9 Apr 2020 00:34:53 -0700 Subject: [PATCH] random-name function --- .ebextensions/lambda-function.config | 48 ------------------- .gitignore | 2 + 2-deploy.sh | 5 ++ _lambda/create-random-name.sh | 1 - _lambda/delete-random-name.sh | 1 - _lambda/random-name.yml | 36 -------------- _lambda/update-random-name.sh | 5 -- {_lambda/random-name => function}/index.js | 0 .../random-name => function}/package.json | 4 +- template.yml | 19 ++++++++ 10 files changed, 27 insertions(+), 94 deletions(-) delete mode 100644 .ebextensions/lambda-function.config delete mode 100755 _lambda/create-random-name.sh delete mode 100755 _lambda/delete-random-name.sh delete mode 100644 _lambda/random-name.yml delete mode 100755 _lambda/update-random-name.sh rename {_lambda/random-name => function}/index.js (100%) rename {_lambda/random-name => function}/package.json (80%) diff --git a/.ebextensions/lambda-function.config b/.ebextensions/lambda-function.config deleted file mode 100644 index 720cd8f..0000000 --- a/.ebextensions/lambda-function.config +++ /dev/null @@ -1,48 +0,0 @@ -commands: - install-npm: - command: sudo -u ec2-user /tmp/install-npm.sh - cwd: /home/ec2-user - test: "[ ! -d /home/ec2-user/.nvm ]" - -packages: - python: - awscli: 1.11.79 -container_commands: - update-random-name: - command: /tmp/update-lambda.sh - leader_only: true - -files: - "/tmp/install-npm.sh": - mode: "000755" - owner: ec2-user - group: ec2-user - content: | - #!/bin/bash - curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash - export NVM_DIR="$HOME/.nvm" - [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" - nvm install 4.3.2 - mkdir -p /home/ec2-user/bin - ln -fs /home/ec2-user/.nvm/versions/node/v4.3.2/bin/npm /home/ec2-user/bin/npm - ln -fs /home/ec2-user/.nvm/versions/node/v4.3.2/bin/node /home/ec2-user/bin/node - - "/tmp/update-lambda.sh": - mode: "000755" - owner: ec2-user - group: ec2-user - content: | - #!/bin/bash - REGION=$(/opt/elasticbeanstalk/bin/get-config environment -k AWS_REGION) - STAGINGDIR=$(/opt/elasticbeanstalk/bin/get-config container -k container_staging_dir) - TOPIC=$(/opt/elasticbeanstalk/bin/get-config environment -k NOTIFICATION_TOPIC) - cd _lambda/random-name - /home/ec2-user/bin/npm install - zip -r ../random-name.zip * - aws lambda update-function-code --function-name random-name --zip-file fileb://../random-name.zip --region $REGION - aws lambda update-function-configuration --function-name random-name --environment "{\"Variables\": {\"TOPIC_ARN\": \"$TOPIC\"} }" --region $REGION - if [ $? -ne 0 ] - then - echo "Lambda function random-name not found" - fi - exit 0 diff --git a/.gitignore b/.gitignore index 86c933c..d233375 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ bucket-name.txt lib _lambda/random-name/node_modules/* out.yml +node_modules +package-lock.json diff --git a/2-deploy.sh b/2-deploy.sh index 1bfd54b..cef6c98 100755 --- a/2-deploy.sh +++ b/2-deploy.sh @@ -1,6 +1,11 @@ #!/bin/bash set -eo pipefail ARTIFACT_BUCKET=$(cat bucket-name.txt) +cd function +rm -rf node_modules +rm -f package-lock.json +npm install --production +cd ../ git archive --format=zip HEAD > package.zip aws cloudformation package --template-file template.yml --s3-bucket $ARTIFACT_BUCKET --output-template-file out.yml aws cloudformation deploy --template-file out.yml --stack-name scorekeep --capabilities CAPABILITY_NAMED_IAM diff --git a/_lambda/create-random-name.sh b/_lambda/create-random-name.sh deleted file mode 100755 index de6cc07..0000000 --- a/_lambda/create-random-name.sh +++ /dev/null @@ -1 +0,0 @@ -aws cloudformation create-stack --stack-name scorekeep-random-name --capabilities CAPABILITY_NAMED_IAM --template-body file://random-name.yml diff --git a/_lambda/delete-random-name.sh b/_lambda/delete-random-name.sh deleted file mode 100755 index bb516be..0000000 --- a/_lambda/delete-random-name.sh +++ /dev/null @@ -1 +0,0 @@ -aws cloudformation delete-stack --stack-name scorekeep-random-name diff --git a/_lambda/random-name.yml b/_lambda/random-name.yml deleted file mode 100644 index 2443b3e..0000000 --- a/_lambda/random-name.yml +++ /dev/null @@ -1,36 +0,0 @@ -Resources: - RandomNameRole: - 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/AWSXrayWriteOnlyAccess - - arn:aws:iam::aws:policy/AmazonSNSFullAccess - - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole - Path: /service-role/ - - RandomNameFunction: - Type: "AWS::Lambda::Function" - Properties: - Code: - ZipFile: "PLACEHOLDER" - Description: "Generate random names" - Environment: - Variables: - TOPIC_ARN: "PLACEHOLDER" - FunctionName: random-name - Handler: index.handler - Role: { "Fn::GetAtt": ["RandomNameRole", "Arn"] } - Runtime: nodejs12.x - Timeout: 7 diff --git a/_lambda/update-random-name.sh b/_lambda/update-random-name.sh deleted file mode 100755 index d62cd0c..0000000 --- a/_lambda/update-random-name.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -cd random-name -npm install -zip -r ../random-name.zip * -aws lambda update-function-code --function-name random-name --zip-file fileb://../random-name.zip diff --git a/_lambda/random-name/index.js b/function/index.js similarity index 100% rename from _lambda/random-name/index.js rename to function/index.js diff --git a/_lambda/random-name/package.json b/function/package.json similarity index 80% rename from _lambda/random-name/package.json rename to function/package.json index 63359ae..74e0df2 100644 --- a/_lambda/random-name/package.json +++ b/function/package.json @@ -3,9 +3,7 @@ "version": "1.0.0", "description": "Name generator", "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, + "scripts": {}, "repository": { "type": "git", "url": "eb-java-scorekeep" diff --git a/template.yml b/template.yml index eab864d..25625b9 100644 --- a/template.yml +++ b/template.yml @@ -1,10 +1,29 @@ AWSTemplateFormatVersion: 2010-09-09 +Transform: AWS::Serverless-2016-10-31 Description: An AWS Elastic application that uses DynamoDB. Parameters: emailAddress: Type: String Default: UPDATEME Resources: + function: + Type: AWS::Serverless::Function + Properties: + FunctionName: random-name + Handler: index.handler + Runtime: nodejs12.x + CodeUri: function/. + Description: Generate random names + Timeout: 10 + # Function's execution role + Policies: + - AWSLambdaBasicExecutionRole + - AWSXrayWriteOnlyAccess + - AmazonSNSFullAccess + Tracing: Active + Environment: + Variables: + TOPIC_ARN: !Ref notificationTopic application: Type: AWS::ElasticBeanstalk::Application Properties: