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

feat(custom-resources): use latest SDK in AwsCustomResource #5442

Merged
merged 6 commits into from
Dec 18, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export interface AwsCustomResourceProps {
/**
* The timeout for the Lambda function implementing this custom resource.
*
* @default Duration.seconds(30)
* @default Duration.seconds(60)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious what the rationale behind the increase is? Is it only to cover the time it'll take to install the latest SDK or were there any other determining factors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's indeed only to cover installation time

*/
readonly timeout?: cdk.Duration
}
Expand Down Expand Up @@ -178,7 +178,7 @@ export class AwsCustomResource extends cdk.Construct implements iam.IGrantable {
handler: 'index.handler',
uuid: '679f53fa-c002-430c-b0da-5b7982bd2287',
lambdaPurpose: 'AWS',
timeout: props.timeout || cdk.Duration.seconds(30),
timeout: props.timeout || cdk.Duration.seconds(60),
role: props.role,
});
this.grantPrincipal = provider.grantPrincipal;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// tslint:disable:no-console
import AWS = require('aws-sdk');
import { execSync } from 'child_process';
import { AwsSdkCall } from '../aws-custom-resource';

/**
Expand Down Expand Up @@ -51,10 +51,39 @@ function filterKeys(object: object, pred: (key: string) => boolean) {
);
}

let latestSdkInstalled = false;

/**
* Installs latest AWS SDK v2
*/
function installLatestSdk(): void {
console.log('Installing latest AWS SDK v2');
// Both HOME and --prefix are needed here because /tmp is the only writable location
execSync('HOME=/tmp npm install aws-sdk@2 --production --no-package-lock --no-save --prefix /tmp');
latestSdkInstalled = true;
}

export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent, context: AWSLambda.Context) {
try {
let AWS: any;
if (!latestSdkInstalled) {
try {
installLatestSdk();
AWS = require('/tmp/node_modules/aws-sdk');
} catch (e) {
console.log(`Failed to install latest AWS SDK v2: ${e}`);
AWS = require('aws-sdk'); // Fallback to pre-installed version
}
} else {
AWS = require('/tmp/node_modules/aws-sdk');
}

if (process.env.USE_NORMAL_SDK) { // For tests only
AWS = require('aws-sdk');
}

console.log(JSON.stringify(event));
console.log('AWS SDK VERSION: ' + (AWS as any).VERSION);
console.log('AWS SDK VERSION: ' + AWS.VERSION);

let physicalResourceId = (event as any).PhysicalResourceId;
let flatData: { [key: string]: string } = {};
Expand Down
4 changes: 3 additions & 1 deletion packages/@aws-cdk/custom-resources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@
"@aws-cdk/aws-s3": "1.19.0",
"@aws-cdk/aws-ssm": "1.19.0",
"@types/aws-lambda": "^8.10.37",
"@types/fs-extra": "^8.0.1",
"@types/sinon": "^7.5.0",
"aws-sdk": "^2.590.0",
"aws-sdk-mock": "^4.5.0",
"cdk-build-tools": "1.19.0",
"cdk-integ-tools": "1.19.0",
"cfn2ts": "1.19.0",
"fs-extra": "^8.1.0",
"nock": "^11.7.0",
"pkglint": "1.19.0",
"sinon": "^7.5.0"
Expand Down Expand Up @@ -124,4 +126,4 @@
"props-default-doc:@aws-cdk/custom-resources.AwsSdkCall.parameters"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SDK = require('aws-sdk');
import AWS = require('aws-sdk-mock');
import fs = require('fs-extra');
import nock = require('nock');
import sinon = require('sinon');
import { AwsSdkCall } from '../../lib';
Expand All @@ -24,9 +25,14 @@ function createRequest(bodyPredicate: (body: AWSLambda.CloudFormationCustomResou
.reply(200);
}

beforeEach(() => {
process.env.USE_NORMAL_SDK = 'true';
});

afterEach(() => {
AWS.restore();
nock.cleanAll();
delete process.env.USE_NORMAL_SDK;
});

test('create event with physical resource id path', async () => {
Expand Down Expand Up @@ -338,3 +344,40 @@ test('flatten correctly flattens a nested object', () => {
'd.1.k.l': false
});
});

test('installs the latest SDK', async () => {
const tmpPath = '/tmp/node_modules/aws-sdk';

fs.remove(tmpPath);

const publishFake = sinon.fake.resolves({});

AWS.mock('SNS', 'publish', publishFake);

const event: AWSLambda.CloudFormationCustomResourceCreateEvent = {
...eventCommon,
RequestType: 'Create',
ResourceProperties: {
ServiceToken: 'token',
Create: {
service: 'SNS',
action: 'publish',
parameters: {
Message: 'message',
TopicArn: 'topic'
},
physicalResourceId: 'id',
} as AwsSdkCall
}
};

const request = createRequest(body =>
body.Status === 'SUCCESS'
);

await handler(event, {} as AWSLambda.Context);

expect(request.isDone()).toBeTruthy();

expect(() => require.resolve(tmpPath)).not.toThrow();
});
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ test('timeout defaults to 30 seconds', () => {

// THEN
expect(stack).toHaveResource('AWS::Lambda::Function', {
Timeout: 30
Timeout: 60
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "AssetParametersba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915S3Bucket7871AF0F"
"Ref": "AssetParameters683d5f66ffd9a473e40d396738e7f632af308796861f4c6495dd2eaf802aea52S3Bucket5D287406"
},
"S3Key": {
"Fn::Join": [
Expand All @@ -122,7 +122,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParametersba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915S3VersionKey106B28FA"
"Ref": "AssetParameters683d5f66ffd9a473e40d396738e7f632af308796861f4c6495dd2eaf802aea52S3VersionKey636C806A"
}
]
}
Expand All @@ -135,7 +135,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParametersba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915S3VersionKey106B28FA"
"Ref": "AssetParameters683d5f66ffd9a473e40d396738e7f632af308796861f4c6495dd2eaf802aea52S3VersionKey636C806A"
}
]
}
Expand All @@ -153,7 +153,7 @@
]
},
"Runtime": "nodejs12.x",
"Timeout": 30
"Timeout": 60
},
"DependsOn": [
"AWS679f53fac002430cb0da5b7982bd2287ServiceRoleDefaultPolicyD28E1A5E",
Expand Down Expand Up @@ -230,17 +230,17 @@
}
},
"Parameters": {
"AssetParametersba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915S3Bucket7871AF0F": {
"AssetParameters683d5f66ffd9a473e40d396738e7f632af308796861f4c6495dd2eaf802aea52S3Bucket5D287406": {
"Type": "String",
"Description": "S3 bucket for asset \"ba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915\""
"Description": "S3 bucket for asset \"683d5f66ffd9a473e40d396738e7f632af308796861f4c6495dd2eaf802aea52\""
},
"AssetParametersba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915S3VersionKey106B28FA": {
"AssetParameters683d5f66ffd9a473e40d396738e7f632af308796861f4c6495dd2eaf802aea52S3VersionKey636C806A": {
"Type": "String",
"Description": "S3 key for asset version \"ba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915\""
"Description": "S3 key for asset version \"683d5f66ffd9a473e40d396738e7f632af308796861f4c6495dd2eaf802aea52\""
},
"AssetParametersba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915ArtifactHashADA31147": {
"AssetParameters683d5f66ffd9a473e40d396738e7f632af308796861f4c6495dd2eaf802aea52ArtifactHash8B257720": {
"Type": "String",
"Description": "Artifact hash for asset \"ba1b614bb55ac13ae885f2efd32be195ffd984700d9dafcccd3102bdba2f9915\""
"Description": "Artifact hash for asset \"683d5f66ffd9a473e40d396738e7f632af308796861f4c6495dd2eaf802aea52\""
}
},
"Outputs": {
Expand Down Expand Up @@ -269,4 +269,4 @@
}
}
}
}
}