Skip to content

Commit 938459f

Browse files
committed
Template and script for Lambda function execution role
1 parent a0dd7ad commit 938459f

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

.ebextensions/lambda-function.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Resources:
1111
TOPIC_ARN: {"Ref" : "NotificationTopic"}
1212
FunctionName: random-name
1313
Handler: index.handler
14-
Role: { "Fn::Join": [ "", [ "arn:aws:iam::",{"Ref" : "AWS::AccountId"},":role/service-role/lambda-scorekeep" ] ] }
14+
Role: { "Fn::Join": [ "", [ "arn:aws:iam::",{"Ref" : "AWS::AccountId"},":role/service-role/scorekeep-lambda" ] ] }
1515
Runtime: nodejs4.3
1616

1717
commands:

README.md

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
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+
5+
## Configuration
6+
A CloudFormation template and [AWS CLI](http://docs.aws.amazon.com/cli/latest/userguide/installing.html) scripts to create and delete the function's execution role are included in the `_lambda` folder:
7+
- `_lambda/lambda-role.yml` - Template that defines the role
8+
- `_lambda/create-lambda-role.sh` - Script to create the role
9+
- `_lambda/delete-lambda-role.sh` - Script to delete the role
10+
11+
Run the script to create the role:
12+
eb-java-scorekeep/_lambda$ ./create-lambda-role.sh
13+
14+
If you don't have the AWS CLI, use the CloudFormation console to create a stack with the template.
15+
16+
Next, add the following policy to your instance profile ([aws-elasticbeanstalk-ec2-role](https://console.aws.amazon.com/iam/home#/roles/aws-elasticbeanstalk-ec2-role)) to let the environment create the Lambda function:
17+
- AWSLambdaFullAccess
18+
19+
Deploy this branch to your Elastic Beanstalk environment. No further configuration is required.
20+
If you don't have an environment, see below.
21+
22+
## Implementation
423
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:
524
- Name: `random-name`
625
- Runtime: Node.js 4.3
@@ -9,14 +28,26 @@ The Lambda function code is included in `_lambda/random-name/index.js`. On deplo
928
- Environment variables:
1029
- REGION_NAME: The region, e.g. `us-east-2`
1130
- TOPIC_ARN: The ARN of an [SNS Topic](https://console.aws.amazon.com/sns/v2/home)
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
31+
- Role named "scorekeep-lambda"
32+
33+
The role named "scorekeep-lambda" has the following policies:
34+
- Managed policy - AWSLambdaBasicExecutionRole
35+
- Managed policy - AmazonSNSFullAccess
36+
- Managed policy - AWSXrayWriteOnlyAccess (optional) for compatibility with the xray branch
37+
- Trust policy -
38+
39+
{
40+
"Version": "2012-10-17",
41+
"Statement": [
42+
{
43+
"Effect": "Allow",
44+
"Principal": {
45+
"Service": "lambda.amazonaws.com"
46+
},
47+
"Action": "sts:AssumeRole"
48+
}
49+
]
50+
}
2051

2152
The Scorekeep API integration is implemented in the following files-
2253
`src/main/java/scorekeep/`
@@ -26,9 +57,6 @@ The Scorekeep API integration is implemented in the following files-
2657
- `UserFactory.java` - **UserFactory.randomNameLambda** Creates the Lambda Invoker with `com.amazonaws.services.lambda.invoke.LambdaInvokerFactory`. Calls the Lambda function to generate a random name.
2758
- `build.gradle` - Adds the Lambda module of the AWS SDK to the Gradle build.
2859

29-
Deploy this branch to your Elastic Beanstalk environment. No further configuration is required.
30-
If you don't have an environment, see below.
31-
3260
# Scorekeep
3361
Scorekeep is a RESTful web API implemented in Java that uses Spring to provide an HTTP interface for creating and managing game sessions and users. This project includes the scorekeep API and a frontend web app that consumes it. The frontend and API can run on the same server and domain or separately, with the API running in Elastic Beanstalk and the frontend served statically by a CDN.
3462

_lambda/lambda-role.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Resources:
2+
RandomNameRole:
3+
Type: "AWS::IAM::Role"
4+
Properties:
5+
AssumeRolePolicyDocument: {
6+
"Version": "2012-10-17",
7+
"Statement": [
8+
{
9+
"Effect": "Allow",
10+
"Principal": {
11+
"Service": "lambda.amazonaws.com"
12+
},
13+
"Action": "sts:AssumeRole"
14+
}
15+
]
16+
}
17+
ManagedPolicyArns:
18+
- arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess
19+
- arn:aws:iam::aws:policy/AmazonSNSFullAccess
20+
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
21+
Path: /service-role/
22+
RoleName: scorekeep-lambda

0 commit comments

Comments
 (0)