Reputation: 155
I have an file GraphQlStack.ts, Where i am declaring my lambda function.
export class GraphqlStack extends Stack {
constructor(scope: App, id: string, props: StackProps){
super(scope, id, props);
const graphqlHandler = new lambda.Function(this, "graphqlHandler", {
runtime: lambda.Runtime.GO_1_X,
functionName: `${STAGE}-graphql`,
code: lambda.Code.fromAsset(Path.join("..", "bin")),
handler: "graphql",
tracing: Tracing.ACTIVE,
timeout: Duration.seconds(60),
memorySize: 512,
vpc: vpc,
vpcSubnets: {
subnets: vpc.privateSubnets,
},
securityGroups: [vpcSecurityGroup],
});
graphqlHandler.addEnvironment("DATABASE_USERNAME", DATABASE_USERNAME || "");
graphqlHandler.addEnvironment("DATABASE_PASSWORD", DATABASE_PASSWORD || "");
}
}
Above code works fine. Now,In another file S3Stack.ts where I am declaring my s3 bucket and giving above function to access s3, I want to add another environment variable.
export class S3Stack extends Stack {
constructor(scope: App, id: string, props: StackProps){
super(scope, id, props);
const bucketName = `${STAGE}-myfiles`
const graphLambda = lambda.Function.fromFunctionAttributes(this, 'Function', {
functionArn: `arn:aws:lambda:${CDK_DEFAULT_REGION}:${CDK_DEFAULT_ACCOUNT}:function:${STAGE}-graphql`,
sameEnvironment: false,
role: lambdaRole
});
graphLambda.addEnvironment("FILES_BUCKET_NAME", bucketName || "");
s3Bucket.grantWrite(graphLambda);
}
}
How I can add this environment variable FILES_BUCKET_NAME with existing lambda graphLambda ?
Note : Values like DATABASE_USERNAME,DATABASE_PASSWORD have been declared correctly in code.
Upvotes: 2
Views: 1347
Reputation: 11512
CDK cannot modify imported resources. Those are just read-only references under the hood. That's why fromFunctionAttributes
returns an IFunction
rather than Function
.
If this all the same app, you can just pass the lambda construct as a prop to another stack, there's no need to use imports.
Upvotes: 2