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

fix(cli): Lambda hotswap fails if environment contains tokens #22099

Merged
merged 4 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 5 additions & 4 deletions packages/aws-cdk/lib/api/hotswap/lambda-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,10 @@ async function isLambdaFunctionCodeOnlyChange(
*/
const tagUpdates: { [tag: string]: string | TagDeletion } = {};
if (updatedProp?.isDifferent) {
updatedProp.newValue.forEach((tag: CfnDiffTagValue) => {
tagUpdates[tag.Key] = tag.Value;
const tasks = updatedProp.newValue.map(async (tag: CfnDiffTagValue) => {
tagUpdates[tag.Key] = await evaluateCfnTemplate.evaluateCfnExpression(tag.Value);
});
await Promise.all(tasks);

updatedProp.oldValue.forEach((tag: CfnDiffTagValue) => {
if (tagUpdates[tag.Key] === undefined) {
Expand All @@ -178,10 +179,10 @@ async function isLambdaFunctionCodeOnlyChange(
}
break;
case 'Description':
description = updatedProp.newValue;
description = await evaluateCfnTemplate.evaluateCfnExpression(updatedProp.newValue);
break;
case 'Environment':
environment = updatedProp.newValue;
environment = await evaluateCfnTemplate.evaluateCfnExpression(updatedProp.newValue);
break;
default:
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -905,3 +905,127 @@ test('calls both updateLambdaCode() and updateLambdaConfiguration() API when it
S3Key: 'new-key',
});
});

test('Lambda hotswap works properly with changes of environment variables, description, tags with tokens', async () => {
// GIVEN
setup.setCurrentCfnStackTemplate({
Resources: {
EventBus: {
Type: 'AWS::Events::EventBus',
Properties: {
Name: 'my-event-bus',
},
},
Func: {
Type: 'AWS::Lambda::Function',
Properties: {
Code: {
S3Bucket: 's3-bucket',
S3Key: 's3-key',
},
FunctionName: 'my-function',
Environment: {
Variables: {
token: { 'Fn::GetAtt': ['EventBus', 'Arn'] },
literal: 'oldValue',
},
},
Description: {
'Fn::Join': ['', [
'oldValue',
{ 'Fn::GetAtt': ['EventBus', 'Arn'] },
]],
},
Tags: [
{
Key: 'token',
Value: { 'Fn::GetAtt': ['EventBus', 'Arn'] },
},
{
Key: 'literal',
Value: 'oldValue',
},
],
},
Metadata: {
'aws:asset:path': 'asset-path',
},
},
},
});

setup.pushStackResourceSummaries(
setup.stackSummaryOf('EventBus', 'AWS::Events::EventBus', 'my-event-bus'),
);

const cdkStackArtifact = setup.cdkStackArtifactOf({
template: {
Resources: {
EventBus: {
Type: 'AWS::Events::EventBus',
Properties: {
Name: 'my-event-bus',
},
},
Func: {
Type: 'AWS::Lambda::Function',
Properties: {
Code: {
S3Bucket: 's3-bucket',
S3Key: 's3-key',
},
FunctionName: 'my-function',
Environment: {
Variables: {
token: { 'Fn::GetAtt': ['EventBus', 'Arn'] },
literal: 'newValue',
},
},
Description: {
'Fn::Join': ['', [
'newValue',
{ 'Fn::GetAtt': ['EventBus', 'Arn'] },
]],
},
Tags: [
{
Key: 'token',
Value: { 'Fn::GetAtt': ['EventBus', 'Arn'] },
},
{
Key: 'literal',
Value: 'newValue',
},
],
},
Metadata: {
'aws:asset:path': 'asset-path',
},
},
},
},
});

// WHEN
const deployStackResult = await hotswapMockSdkProvider.tryHotswapDeployment(cdkStackArtifact);

// THEN
expect(deployStackResult).not.toBeUndefined();
expect(mockUpdateLambdaConfiguration).toHaveBeenCalledWith({
FunctionName: 'my-function',
Environment: {
Variables: {
token: 'arn:aws:events:here:123456789012:event-bus/my-event-bus',
literal: 'newValue',
},
},
Description: 'newValuearn:aws:events:here:123456789012:event-bus/my-event-bus',
});
expect(mockTagResource).toHaveBeenCalledWith({
Resource: 'arn:aws:lambda:here:123456789012:function:my-function',
Tags: {
token: 'arn:aws:events:here:123456789012:event-bus/my-event-bus',
literal: 'newValue',
},
});
});