Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow lambdas to take additional permissions as part of their properties! #16

Merged
merged 3 commits into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions packages/aws-cdk-lambda/lib/lambda.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Construct, ServicePrincipal, Token } from 'aws-cdk';
import { Arn, Construct, PolicyStatement, ServicePrincipal, Token } from 'aws-cdk';
import { Role } from 'aws-cdk-iam';
import { lambda } from 'aws-cdk-resources';
import { LambdaCode } from './code';
Expand Down Expand Up @@ -70,6 +70,13 @@ export interface LambdaProps {
* @default The default value is 128 MB
*/
memorySize?: number;

/**
* Initial policy statements to add to the created Lambda Role.
*
* You can call `addToRolePolicy` to the created lambda to add statements post creation.
*/
initialPolicy?: PolicyStatement[];
}

/**
Expand Down Expand Up @@ -113,9 +120,20 @@ export class Lambda extends LambdaRef {

this.role = new Role(this, 'ServiceRole', {
assumedBy: new ServicePrincipal('lambda.amazonaws.com'),
managedPolicyArns: [ 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' ],
// the arn is in the form of - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
managedPolicyArns: [ Arn.fromComponents({
service: "iam",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use single quotes instead of double quotes

region: "", // no region for managed policy
account: "aws", // the account for a managed policy is 'aws'
resource: "policy",
resourceName: "service-role/AWSLambdaBasicExecutionRole",
})],
});

for (const statement of (props.initialPolicy || [])) {
this.role.addToPolicy(statement);
}

const resource = new lambda.FunctionResource(this, 'Resource', {
functionName: props.functionName,
description: props.description,
Expand Down
3 changes: 2 additions & 1 deletion packages/aws-cdk-lambda/test/inline.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
{"Fn::Join": ["", ["arn", ":", {"Ref": "AWS::Partition"}, ":", "iam", ":", "", ":", "aws", ":", "policy", "/", "service-role/AWSLambdaBasicExecutionRole"]]}

]
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk-lambda/test/integ.inline.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
{"Fn::Join": ["", ["arn", ":", {"Ref": "AWS::Partition"}, ":", "iam", ":", "", ":", "aws", ":", "policy", "/", "service-role/AWSLambdaBasicExecutionRole"]]}
]
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk-lambda/test/integ.lambda.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
{"Fn::Join": ["", ["arn", ":", {"Ref": "AWS::Partition"}, ":", "iam", ":", "", ":", "aws", ":", "policy", "/", "service-role/AWSLambdaBasicExecutionRole"]]}
]
}
},
Expand Down
68 changes: 63 additions & 5 deletions packages/aws-cdk-lambda/test/test.lambda.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccountPrincipal, Arn, ArnPrincipal, AwsAccountId, Construct, ServicePrincipal, Stack } from 'aws-cdk';
import { AccountPrincipal, Arn, ArnPrincipal, AwsAccountId, Construct, PolicyStatement, ServicePrincipal, Stack } from 'aws-cdk';
import { expect } from 'aws-cdk-assert';
import { Test } from 'nodeunit';
import { Lambda, LambdaInlineCode, LambdaRuntime } from '../lib';
Expand Down Expand Up @@ -26,7 +26,10 @@ export = {
Principal: { Service: 'lambda.amazonaws.com' } } ],
Version: '2012-10-17' },
ManagedPolicyArns:
[ 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' ] } },
// arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
// tslint:disable-next-line:max-line-length
[{'Fn::Join': ['', ['arn', ':', {Ref: 'AWS::Partition'}, ':', 'iam', ':', '', ':', 'aws', ':', 'policy', '/', 'service-role/AWSLambdaBasicExecutionRole']]}],
}},
MyLambdaCCE802FB:
{ Type: 'AWS::Lambda::Function',
Properties:
Expand All @@ -38,6 +41,61 @@ export = {
test.done();
},

'adds policy permissions'(test: Test) {
const stack = new Stack();
new Lambda(stack, 'MyLambda', {
code: new LambdaInlineCode('foo'),
handler: 'index.handler',
runtime: LambdaRuntime.NodeJS610,
initialPolicy: [new PolicyStatement().addAction("*").addResource("*")]
});
expect(stack).toMatch({ Resources:
{ MyLambdaServiceRole4539ECB6:
{ Type: 'AWS::IAM::Role',
Properties:
{ AssumeRolePolicyDocument:
{ Statement:
[ { Action: 'sts:AssumeRole',
Effect: 'Allow',
Principal: { Service: 'lambda.amazonaws.com' } } ],
Version: '2012-10-17' },
ManagedPolicyArns:
// tslint:disable-next-line:max-line-length
[{'Fn::Join': ['', ['arn', ':', {Ref: 'AWS::Partition'}, ':', 'iam', ':', '', ':', 'aws', ':', 'policy', '/', 'service-role/AWSLambdaBasicExecutionRole']]}],
}},
MyLambdaServiceRoleDefaultPolicy5BBC6F68: {
Type: "AWS::IAM::Policy",
Properties: {
PolicyDocument: {
Statement: [
{
Action: "*",
Effect: "Allow",
Resource: "*"
}
],
Version: "2012-10-17"
},
PolicyName: "MyLambdaServiceRoleDefaultPolicy5BBC6F68",
Roles: [
{
Ref: "MyLambdaServiceRole4539ECB6"
}
]
}
},
MyLambdaCCE802FB:
{ Type: 'AWS::Lambda::Function',
Properties:
{ Code: { ZipFile: 'foo' },
Handler: 'index.handler',
Role: { 'Fn::GetAtt': [ 'MyLambdaServiceRole4539ECB6', 'Arn' ] },
Runtime: 'nodejs6.10' },
DependsOn: [ 'MyLambdaServiceRole4539ECB6', 'MyLambdaServiceRoleDefaultPolicy5BBC6F68' ] } } } );
test.done();

},

'fails if inline code is used for an invalid runtime'(test: Test) {
const stack = new Stack();
test.throws(() => new Lambda(stack, 'MyLambda', {
Expand Down Expand Up @@ -77,9 +135,9 @@ export = {
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
"ManagedPolicyArns":
// tslint:disable-next-line:max-line-length
[{'Fn::Join': ['', ['arn', ':', {Ref: 'AWS::Partition'}, ':', 'iam', ':', '', ':', 'aws', ':', 'policy', '/', 'service-role/AWSLambdaBasicExecutionRole']]}],
}
},
"MyLambdaCCE802FB": {
Expand Down