Created
January 31, 2019 01:34
-
-
Save allumbra/57b9f923a5e81ef83ebb6fefe1480de3 to your computer and use it in GitHub Desktop.
LambdaA to invoke LambdaB - circular reference when trying to include LambdaB function name in LambdaA environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const s3PutLambdaRole = new iam.Role(this, "S3PutLambdaRole", { | |
assumedBy: new iam.ServicePrincipal("lambda.amazonaws.com") | |
// managedPolicyArns: | |
}); | |
bucket.grantRead(s3PutLambdaRole); | |
const s3EventLambda = new lambda.Function(this, "s3PutHandler", { | |
runtime: lambda.Runtime.NodeJS810, | |
code: lambda.Code.directory("resources"), | |
role: s3PutLambdaRole, | |
handler: "cache.processEvent", | |
memorySize: 1000, | |
timeout: 60, | |
environment: { | |
BUCKET: bucket.bucketName // provides bucket name to runtime | |
} | |
}); | |
s3EventLambda.addEventSource( | |
new S3EventSource(bucket, { | |
events: [s3.EventType.ObjectCreated] | |
}) | |
); | |
//////////////// Fanout Lambda | |
const dynamoLambdaRole = new iam.Role(this, "DynamoLambdaRole", { | |
assumedBy: new iam.ServicePrincipal("lambda.amazonaws.com") | |
}); | |
dynamoLambdaRole.addToPolicy( | |
new iam.PolicyStatement() | |
.addAction("dynamoDB:*") | |
.addResource(table.tableArn) | |
); | |
const fanoutLambda = new lambda.Function(this, "fanoutWorker", { | |
runtime: lambda.Runtime.NodeJS810, | |
code: lambda.Code.directory("chunk_processor"), | |
role: dynamoLambdaRole, | |
handler: "index.processChunk", | |
memorySize: 1000, | |
timeout: 60, | |
environment: { | |
table: table.tableName | |
} | |
}); | |
// allow s3PutLambda to call fanout lambda | |
s3PutLambdaRole.addToPolicy( | |
new iam.PolicyStatement() | |
.addAction("lambda:InvokeFunction") | |
.addResource(fanoutLambda.functionArn) | |
); | |
// pass fanoutLambda name to s3Event | |
s3EventLambda.addEnvironment( | |
"fanoutLambda", fanoutLambda.functionName // circular ref here | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment