-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): support hotswapping Lambda functions with inline code (#18408
) Similarly to #18319, this PR supports hotswap of Lambda functions that use `InlineCode`. `InlineCode` uses [CloudFormation `ZipFile` feature](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html#:~:text=requires%3A%20No%20interruption-,ZipFile,-\(Node.js%20and). To emulate its behavior, we create a zip file of the provided inline code with its filename `index.js` or `index.py` according to the runtime (CFn only supports python or nodejs for ZipFile), and pass the file's binary buffer to Lambda API. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
3 changed files
with
220 additions
and
3 deletions.
There are no files selected for viewing
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
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
148 changes: 148 additions & 0 deletions
148
packages/aws-cdk/test/api/hotswap/lambda-functions-inline-hotswap-deployments.test.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { Lambda } from 'aws-sdk'; | ||
import * as setup from './hotswap-test-setup'; | ||
|
||
let mockUpdateLambdaCode: (params: Lambda.Types.UpdateFunctionCodeRequest) => Lambda.Types.FunctionConfiguration; | ||
let mockTagResource: (params: Lambda.Types.TagResourceRequest) => {}; | ||
let mockUntagResource: (params: Lambda.Types.UntagResourceRequest) => {}; | ||
let hotswapMockSdkProvider: setup.HotswapMockSdkProvider; | ||
|
||
beforeEach(() => { | ||
hotswapMockSdkProvider = setup.setupHotswapTests(); | ||
mockUpdateLambdaCode = jest.fn(); | ||
mockTagResource = jest.fn(); | ||
mockUntagResource = jest.fn(); | ||
hotswapMockSdkProvider.stubLambda({ | ||
updateFunctionCode: mockUpdateLambdaCode, | ||
tagResource: mockTagResource, | ||
untagResource: mockUntagResource, | ||
}); | ||
}); | ||
|
||
test('calls the updateLambdaCode() API when it receives only a code difference in a Lambda function (Inline Node.js code)', async () => { | ||
// GIVEN | ||
setup.setCurrentCfnStackTemplate({ | ||
Resources: { | ||
Func: { | ||
Type: 'AWS::Lambda::Function', | ||
Properties: { | ||
Code: { | ||
ZipFile: 'exports.handler = () => {return true}', | ||
}, | ||
Runtime: 'nodejs14.x', | ||
FunctionName: 'my-function', | ||
}, | ||
}, | ||
}, | ||
}); | ||
const newCode = 'exports.handler = () => {return false}'; | ||
const cdkStackArtifact = setup.cdkStackArtifactOf({ | ||
template: { | ||
Resources: { | ||
Func: { | ||
Type: 'AWS::Lambda::Function', | ||
Properties: { | ||
Code: { | ||
ZipFile: newCode, | ||
}, | ||
Runtime: 'nodejs14.x', | ||
FunctionName: 'my-function', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// WHEN | ||
const deployStackResult = await hotswapMockSdkProvider.tryHotswapDeployment(cdkStackArtifact); | ||
|
||
// THEN | ||
expect(deployStackResult).not.toBeUndefined(); | ||
expect(mockUpdateLambdaCode).toHaveBeenCalledWith({ | ||
FunctionName: 'my-function', | ||
ZipFile: expect.any(Buffer), | ||
}); | ||
}); | ||
|
||
test('calls the updateLambdaCode() API when it receives only a code difference in a Lambda function (Inline Python code)', async () => { | ||
// GIVEN | ||
setup.setCurrentCfnStackTemplate({ | ||
Resources: { | ||
Func: { | ||
Type: 'AWS::Lambda::Function', | ||
Properties: { | ||
Code: { | ||
ZipFile: 'def handler(event, context):\n return True', | ||
}, | ||
Runtime: 'python3.9', | ||
FunctionName: 'my-function', | ||
}, | ||
}, | ||
}, | ||
}); | ||
const cdkStackArtifact = setup.cdkStackArtifactOf({ | ||
template: { | ||
Resources: { | ||
Func: { | ||
Type: 'AWS::Lambda::Function', | ||
Properties: { | ||
Code: { | ||
ZipFile: 'def handler(event, context):\n return False', | ||
}, | ||
Runtime: 'python3.9', | ||
FunctionName: 'my-function', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// WHEN | ||
const deployStackResult = await hotswapMockSdkProvider.tryHotswapDeployment(cdkStackArtifact); | ||
|
||
// THEN | ||
expect(deployStackResult).not.toBeUndefined(); | ||
expect(mockUpdateLambdaCode).toHaveBeenCalledWith({ | ||
FunctionName: 'my-function', | ||
ZipFile: expect.any(Buffer), | ||
}); | ||
}); | ||
|
||
test('throw a CfnEvaluationException when it receives an unsupported function runtime', async () => { | ||
// GIVEN | ||
setup.setCurrentCfnStackTemplate({ | ||
Resources: { | ||
Func: { | ||
Type: 'AWS::Lambda::Function', | ||
Properties: { | ||
Code: { | ||
ZipFile: 'def handler(event:, context:) true end', | ||
}, | ||
Runtime: 'ruby2.7', | ||
FunctionName: 'my-function', | ||
}, | ||
}, | ||
}, | ||
}); | ||
const cdkStackArtifact = setup.cdkStackArtifactOf({ | ||
template: { | ||
Resources: { | ||
Func: { | ||
Type: 'AWS::Lambda::Function', | ||
Properties: { | ||
Code: { | ||
ZipFile: 'def handler(event:, context:) false end', | ||
}, | ||
Runtime: 'ruby2.7', | ||
FunctionName: 'my-function', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// WHEN | ||
const tryHotswap = hotswapMockSdkProvider.tryHotswapDeployment(cdkStackArtifact); | ||
|
||
// THEN | ||
await expect(tryHotswap).rejects.toThrow('runtime ruby2.7 is unsupported, only node.js and python runtimes are currently supported.'); | ||
}); |