Skip to content

Commit acc536a

Browse files
committed
create and update Lambda function on deploy
1 parent 921d884 commit acc536a

File tree

3 files changed

+69
-13
lines changed

3 files changed

+69
-13
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Resources:
2+
RandomNameFunction:
3+
Type: "AWS::Lambda::Function"
4+
Properties:
5+
Code:
6+
ZipFile: "PLACEHOLDER"
7+
Description: "Generate random names"
8+
Environment:
9+
Variables:
10+
REGION_NAME: {"Ref" : "AWS::Region"}
11+
TOPIC_ARN: {"Ref" : "NotificationTopic"}
12+
FunctionName: random-name
13+
Handler: index.handler
14+
Role: { "Fn::Join": [ "", [ "arn:aws:iam::",{"Ref" : "AWS::AccountId"},":role/service-role/lambda-scorekeep" ] ] }
15+
Runtime: nodejs4.3
16+
17+
packages:
18+
yum:
19+
jq: []
20+
git: []
21+
22+
commands:
23+
install-npm:
24+
command: sudo -u ec2-user /tmp/install-npm.sh
25+
cwd: /home/ec2-user
26+
test: "[ ! -d /home/ec2-user/.nvm ]"
27+
28+
container_commands:
29+
update-random-name:
30+
command: /tmp/update-lambda.sh
31+
leader_only: true
32+
33+
files:
34+
"/tmp/install-npm.sh":
35+
mode: "000755"
36+
owner: ec2-user
37+
group: ec2-user
38+
content: |
39+
#!/bin/bash
40+
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
41+
export NVM_DIR="$HOME/.nvm"
42+
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
43+
nvm install 4.3.2
44+
mkdir -p /home/ec2-user/bin
45+
ln -fs /home/ec2-user/.nvm/versions/node/v4.3.2/bin/npm /home/ec2-user/bin/npm
46+
ln -fs /home/ec2-user/.nvm/versions/node/v4.3.2/bin/node /home/ec2-user/bin/node
47+
48+
"/tmp/update-lambda.sh":
49+
mode: "000755"
50+
owner: ec2-user
51+
group: ec2-user
52+
content: |
53+
#!/bin/bash
54+
REGION=$(/opt/elasticbeanstalk/bin/get-config environment | jq -r '.AWS_REGION')
55+
STAGINGDIR=$(/opt/elasticbeanstalk/bin/get-config container -k container_staging_dir)
56+
cd _lambda/random-name
57+
/home/ec2-user/bin/npm install
58+
zip -r ../random-name.zip *
59+
aws lambda update-function-code --function-name random-name --zip-file fileb://../random-name.zip --region $REGION

README.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
# Lambda integration
22
This branch uses a Node.js Lambda function to generate random names for new users, instead of calling a web API. The Scorekeep API uses the AWS SDK to invoke the Lambda by function name (`random-name`) with Bean classes to represent (and serialize to/from) the input and output JSON.
33

4-
The Lambda function code is included in `_lambda/random-name/index.js`. Create a Lambda function with the following settings:
5-
- Template: blank function
6-
- Triggers: none
4+
The Lambda function code is included in `_lambda/random-name/index.js`. On deploy, configuration files in the .ebextensions folder create a function with the following settings:
75
- Name: `random-name`
86
- Runtime: Node.js 4.3
97
- Description: `Generate random names`
108
- Code: in `_Lambda/random-name`
11-
- Run `npm install`
12-
- Run `zip -r ../random-name.zip *`
13-
- Upload the ZIP archive
149
- Environment variables:
1510
- REGION_NAME: The region, e.g. `us-east-2`
1611
- TOPIC_ARN: The ARN of an [SNS Topic](https://console.aws.amazon.com/sns/v2/home)
17-
- Role with permission to call SNS
18-
- Test event:
19-
```
20-
{
21-
"userid": "1ABCDEFG"
22-
}
23-
```
12+
- Role named "lambda-scorekeep"
13+
14+
Create a role named "lambda-scorekeep" in the IAM management console and add the following policies:
15+
- AWSLambdaBasicExecutionRole
16+
- AmazonSNSFullAccess
17+
18+
While you are there, add the following policy to your instance profile (aws-elasticbeanstalk-ec2-role) to let the environment create the Lambda function:
19+
- AWSLambdaFullAccess
2420

2521
The Scorekeep API integration is implemented in the following files-
2622
`src/main/java/scorekeep/`

_lambda/update-random-name.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/bin/bash
12
cd random-name
23
npm install
34
zip -r ../random-name.zip *

0 commit comments

Comments
 (0)