Skip to content

Commit

Permalink
feat(custom-resources): optional latest SDK installation for AwsCusto…
Browse files Browse the repository at this point in the history
…mResource (#9515)

Fix latest AWS SDK installation for `AwsCustomResource` and add option to
disable it.

Closes #9289
Closes #9322


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jogold committed Aug 17, 2020
1 parent 97e44a7 commit effdd3c
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 46 deletions.
Expand Up @@ -74,7 +74,8 @@
"Ref": "UserPoolDomainD0EA232A"
}
}
}
},
"InstallLatestAwsSdk": true
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down Expand Up @@ -136,7 +137,7 @@
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "AssetParameters8ae75ec4aaae0510b0918d3a69fac5c978d780ae0d60bb94c65c7f5b4c498061S3Bucket67234880"
"Ref": "AssetParametersd731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557S3BucketA250C084"
},
"S3Key": {
"Fn::Join": [
Expand All @@ -149,7 +150,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParameters8ae75ec4aaae0510b0918d3a69fac5c978d780ae0d60bb94c65c7f5b4c498061S3VersionKey9802AE96"
"Ref": "AssetParametersd731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557S3VersionKeyDC4F0CD7"
}
]
}
Expand All @@ -162,7 +163,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParameters8ae75ec4aaae0510b0918d3a69fac5c978d780ae0d60bb94c65c7f5b4c498061S3VersionKey9802AE96"
"Ref": "AssetParametersd731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557S3VersionKeyDC4F0CD7"
}
]
}
Expand Down Expand Up @@ -204,17 +205,17 @@
}
},
"Parameters": {
"AssetParameters8ae75ec4aaae0510b0918d3a69fac5c978d780ae0d60bb94c65c7f5b4c498061S3Bucket67234880": {
"AssetParametersd731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557S3BucketA250C084": {
"Type": "String",
"Description": "S3 bucket for asset \"8ae75ec4aaae0510b0918d3a69fac5c978d780ae0d60bb94c65c7f5b4c498061\""
"Description": "S3 bucket for asset \"d731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557\""
},
"AssetParameters8ae75ec4aaae0510b0918d3a69fac5c978d780ae0d60bb94c65c7f5b4c498061S3VersionKey9802AE96": {
"AssetParametersd731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557S3VersionKeyDC4F0CD7": {
"Type": "String",
"Description": "S3 key for asset version \"8ae75ec4aaae0510b0918d3a69fac5c978d780ae0d60bb94c65c7f5b4c498061\""
"Description": "S3 key for asset version \"d731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557\""
},
"AssetParameters8ae75ec4aaae0510b0918d3a69fac5c978d780ae0d60bb94c65c7f5b4c498061ArtifactHash9212BF97": {
"AssetParametersd731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557ArtifactHash5701DE73": {
"Type": "String",
"Description": "Artifact hash for asset \"8ae75ec4aaae0510b0918d3a69fac5c978d780ae0d60bb94c65c7f5b4c498061\""
"Description": "Artifact hash for asset \"d731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557\""
}
}
}
23 changes: 12 additions & 11 deletions packages/@aws-cdk/aws-ecr/test/integ.imagescan.expected.json
Expand Up @@ -73,7 +73,8 @@
]
}
}
}
},
"InstallLatestAwsSdk": true
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down Expand Up @@ -164,7 +165,7 @@
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "AssetParametersa75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4S3BucketC6CBC09E"
"Ref": "AssetParametersd731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557S3BucketA250C084"
},
"S3Key": {
"Fn::Join": [
Expand All @@ -177,7 +178,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParametersa75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4S3VersionKeyB194AB23"
"Ref": "AssetParametersd731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557S3VersionKeyDC4F0CD7"
}
]
}
Expand All @@ -190,7 +191,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParametersa75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4S3VersionKeyB194AB23"
"Ref": "AssetParametersd731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557S3VersionKeyDC4F0CD7"
}
]
}
Expand All @@ -217,17 +218,17 @@
}
},
"Parameters": {
"AssetParametersa75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4S3BucketC6CBC09E": {
"AssetParametersd731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557S3BucketA250C084": {
"Type": "String",
"Description": "S3 bucket for asset \"a75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4\""
"Description": "S3 bucket for asset \"d731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557\""
},
"AssetParametersa75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4S3VersionKeyB194AB23": {
"AssetParametersd731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557S3VersionKeyDC4F0CD7": {
"Type": "String",
"Description": "S3 key for asset version \"a75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4\""
"Description": "S3 key for asset version \"d731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557\""
},
"AssetParametersa75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4ArtifactHashBE5BD63C": {
"AssetParametersd731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557ArtifactHash5701DE73": {
"Type": "String",
"Description": "Artifact hash for asset \"a75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4\""
"Description": "Artifact hash for asset \"d731b1475f16a318a48a76c83d255f7422cfa5f025c5bff93537b8f0b8e94557\""
}
},
"Outputs": {
Expand Down Expand Up @@ -282,4 +283,4 @@
}
}
}
}
}
Expand Up @@ -55,6 +55,7 @@ test('custom resource exists', () => {
responsePath: 'SecurityGroups.0.GroupId',
},
},
InstallLatestAwsSdk: true,
},
DependsOn: [
'GroupC77FDACD',
Expand Down Expand Up @@ -100,4 +101,4 @@ test('can create security group rule', () => {
},
ToPort: 443,
}));
});
});
5 changes: 5 additions & 0 deletions packages/@aws-cdk/custom-resources/README.md
Expand Up @@ -327,6 +327,11 @@ Path to data must be specified using a dot notation, e.g. to get the string valu
of the `Title` attribute for the first item returned by `dynamodb.query` it should
be `Items.0.Title.S`.

To make sure that the newest API calls are available the latest AWS SDK v2 is installed
in the Lambda function implementing the custom resource. The installation takes around 60
seconds. If you prefer to optimize for speed, you can disable the installation by setting
the `installLatestAwsSdk` prop to `false`.

### Execution Policy

You must provide the `policy` property defining the IAM Policy that will be applied to the API calls.
Expand Down
Expand Up @@ -244,6 +244,16 @@ export interface AwsCustomResourceProps {
* @default logs.RetentionDays.INFINITE
*/
readonly logRetention?: logs.RetentionDays;

/**
* Whether to install the latest AWS SDK v2. Allows to use the latest API
* calls documented at https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/index.html.
*
* The installation takes around 60 seconds.
*
* @default true
*/
readonly installLatestAwsSdk?: boolean;
}

/**
Expand Down Expand Up @@ -332,6 +342,7 @@ export class AwsCustomResource extends cdk.Construct implements iam.IGrantable {
create: create && encodeBooleans(create),
update: props.onUpdate && encodeBooleans(props.onUpdate),
delete: props.onDelete && encodeBooleans(props.onDelete),
installLatestAwsSdk: props.installLatestAwsSdk ?? true,
},
});
}
Expand Down
Expand Up @@ -64,27 +64,24 @@ 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');
execSync('tree /tmp/node_modules');
latestSdkInstalled = true;
}

/* eslint-disable @typescript-eslint/no-require-imports, import/no-extraneous-dependencies */
export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent, context: AWSLambda.Context) {
try {
let AWS: any;
if (!latestSdkInstalled) {
if (!latestSdkInstalled && event.ResourceProperties.InstallLatestAwsSdk === 'true') {
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 {
} else if (latestSdkInstalled) {
AWS = require('/tmp/node_modules/aws-sdk');
}

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

Expand Down
Expand Up @@ -8,8 +8,6 @@ import { flatten, handler, forceSdkInstallation } from '../../lib/aws-custom-res

/* eslint-disable no-console */

AWS.setSDK(require.resolve('aws-sdk'));

console.log = jest.fn();

const eventCommon = {
Expand All @@ -28,13 +26,12 @@ function createRequest(bodyPredicate: (body: AWSLambda.CloudFormationCustomResou
}

beforeEach(() => {
process.env.USE_NORMAL_SDK = 'true';
AWS.setSDK(require.resolve('aws-sdk'));
});

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

test('create event with physical resource id path', async () => {
Expand Down Expand Up @@ -417,7 +414,13 @@ test('flatten correctly flattens a nested object', () => {
test('installs the latest SDK', async () => {
const tmpPath = '/tmp/node_modules/aws-sdk';

await fs.remove(tmpPath);
// Symlink to normal SDK to be able to call AWS.setSDK()
await fs.ensureDir('/tmp/node_modules');
await fs.symlink(require.resolve('aws-sdk'), tmpPath);
AWS.setSDK(tmpPath);

// Now remove the symlink and let the handler install it
await fs.unlink(tmpPath);

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

Expand All @@ -437,6 +440,7 @@ test('installs the latest SDK', async () => {
},
physicalResourceId: PhysicalResourceId.of('id'),
} as AwsSdkCall,
InstallLatestAwsSdk: 'true',
},
};

Expand Down
Expand Up @@ -52,6 +52,7 @@ test('aws sdk js custom resource with onCreate and onDelete', () => {
'logGroupName': '/aws/lambda/loggroup',
},
},
'InstallLatestAwsSdk': true,
});

expect(stack).toHaveResource('AWS::IAM::Policy', {
Expand Down Expand Up @@ -521,3 +522,24 @@ test('can specify log retention', () => {
RetentionInDays: 7,
});
});

test('disable AWS SDK installation', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new AwsCustomResource(stack, 'AwsSdk', {
installLatestAwsSdk: false,
onCreate: {
service: 'service',
action: 'action',
physicalResourceId: PhysicalResourceId.of('id'),
},
policy: AwsCustomResourcePolicy.fromSdkCalls({ resources: AwsCustomResourcePolicy.ANY_RESOURCE }),
});

// THEN
expect(stack).toHaveResource('Custom::AWS', {
'InstallLatestAwsSdk': false,
});
});
Expand Up @@ -41,7 +41,8 @@
"Ref": "TopicBFC7AF6E"
}
}
}
},
"InstallLatestAwsSdk": true
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down Expand Up @@ -113,7 +114,7 @@
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "AssetParametersa75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4S3BucketC6CBC09E"
"Ref": "AssetParameters0fb8d27cb9854a09fcc93f73882cc5df12ac4108ab02581ab449da4df345f68bS3BucketA4761502"
},
"S3Key": {
"Fn::Join": [
Expand All @@ -126,7 +127,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParametersa75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4S3VersionKeyB194AB23"
"Ref": "AssetParameters0fb8d27cb9854a09fcc93f73882cc5df12ac4108ab02581ab449da4df345f68bS3VersionKeyB049B315"
}
]
}
Expand All @@ -139,7 +140,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParametersa75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4S3VersionKeyB194AB23"
"Ref": "AssetParameters0fb8d27cb9854a09fcc93f73882cc5df12ac4108ab02581ab449da4df345f68bS3VersionKeyB049B315"
}
]
}
Expand Down Expand Up @@ -186,7 +187,8 @@
"physicalResourceId": {
"responsePath": "Topics.0.TopicArn"
}
}
},
"InstallLatestAwsSdk": true
},
"DependsOn": [
"TopicBFC7AF6E"
Expand Down Expand Up @@ -235,24 +237,25 @@
"physicalResourceId": {
"responsePath": "Parameter.ARN"
}
}
},
"InstallLatestAwsSdk": true
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
}
},
"Parameters": {
"AssetParametersa75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4S3BucketC6CBC09E": {
"AssetParameters0fb8d27cb9854a09fcc93f73882cc5df12ac4108ab02581ab449da4df345f68bS3BucketA4761502": {
"Type": "String",
"Description": "S3 bucket for asset \"a75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4\""
"Description": "S3 bucket for asset \"0fb8d27cb9854a09fcc93f73882cc5df12ac4108ab02581ab449da4df345f68b\""
},
"AssetParametersa75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4S3VersionKeyB194AB23": {
"AssetParameters0fb8d27cb9854a09fcc93f73882cc5df12ac4108ab02581ab449da4df345f68bS3VersionKeyB049B315": {
"Type": "String",
"Description": "S3 key for asset version \"a75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4\""
"Description": "S3 key for asset version \"0fb8d27cb9854a09fcc93f73882cc5df12ac4108ab02581ab449da4df345f68b\""
},
"AssetParametersa75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4ArtifactHashBE5BD63C": {
"AssetParameters0fb8d27cb9854a09fcc93f73882cc5df12ac4108ab02581ab449da4df345f68bArtifactHash840E5880": {
"Type": "String",
"Description": "Artifact hash for asset \"a75563f489fb6bc4064bc85b91ef607f671326e647bcd9d9bcab0731de62edd4\""
"Description": "Artifact hash for asset \"0fb8d27cb9854a09fcc93f73882cc5df12ac4108ab02581ab449da4df345f68b\""
}
},
"Outputs": {
Expand Down Expand Up @@ -281,4 +284,4 @@
}
}
}
}
}

0 comments on commit effdd3c

Please sign in to comment.