Skip to content

Instantly share code, notes, and snippets.

@allumbra
Created January 31, 2019 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allumbra/57b9f923a5e81ef83ebb6fefe1480de3 to your computer and use it in GitHub Desktop.
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
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