Skip to content

Commit

Permalink
feat(ec2): add region parameter for UserData via addS3DownloadCommand (
Browse files Browse the repository at this point in the history
…#16667)

Allow the specification of a region in `addS3DownloadCommand()` in the UserData helper.

Fixes #8287


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
LukvonStrom authored Oct 25, 2021
1 parent 443a23e commit 691d377
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-ec2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,7 @@ const instance = new ec2.Instance(this, 'Instance', {
const localPath = instance.userData.addS3DownloadCommand({
bucket:asset.bucket,
bucketKey:asset.s3ObjectKey,
region: 'us-east-1', // Optional
});
instance.userData.addExecuteFileCommand({
filePath:localPath,
Expand Down
10 changes: 8 additions & 2 deletions packages/@aws-cdk/aws-ec2/lib/user-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export interface S3DownloadOptions {
*/
readonly localFile?: string;

/**
* The region of the S3 Bucket (needed for access via VPC Gateway)
* @default none
*/
readonly region?: string

}

/**
Expand Down Expand Up @@ -156,7 +162,7 @@ class LinuxUserData extends UserData {
const localPath = ( params.localFile && params.localFile.length !== 0 ) ? params.localFile : `/tmp/${ params.bucketKey }`;
this.addCommands(
`mkdir -p $(dirname '${localPath}')`,
`aws s3 cp '${s3Path}' '${localPath}'`,
`aws s3 cp '${s3Path}' '${localPath}'` + (params.region !== undefined ? ` --region ${params.region}` : ''),
);

return localPath;
Expand Down Expand Up @@ -215,7 +221,7 @@ class WindowsUserData extends UserData {
const localPath = ( params.localFile && params.localFile.length !== 0 ) ? params.localFile : `C:/temp/${ params.bucketKey }`;
this.addCommands(
`mkdir (Split-Path -Path '${localPath}' ) -ea 0`,
`Read-S3Object -BucketName '${params.bucket.bucketName}' -key '${params.bucketKey}' -file '${localPath}' -ErrorAction Stop`,
`Read-S3Object -BucketName '${params.bucket.bucketName}' -key '${params.bucketKey}' -file '${localPath}' -ErrorAction Stop` + (params.region !== undefined ? ` -Region ${params.region}` : ''),
);
return localPath;
}
Expand Down
59 changes: 59 additions & 0 deletions packages/@aws-cdk/aws-ec2/test/userdata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,35 @@ describe('user data', () => {
'Read-S3Object -BucketName \'test2\' -key \'filename2.bat\' -file \'c:\\test\\location\\otherScript.bat\' -ErrorAction Stop</powershell>',
);

});
test('can windows userdata download S3 files with given region', () => {
// GIVEN
const stack = new Stack();
const userData = ec2.UserData.forWindows();
const bucket = Bucket.fromBucketName( stack, 'testBucket', 'test' );
const bucket2 = Bucket.fromBucketName( stack, 'testBucket2', 'test2' );

// WHEN
userData.addS3DownloadCommand({
bucket,
bucketKey: 'filename.bat',
region: 'us-east-1',
} );
userData.addS3DownloadCommand({
bucket: bucket2,
bucketKey: 'filename2.bat',
localFile: 'c:\\test\\location\\otherScript.bat',
region: 'us-east-1',
} );

// THEN
const rendered = userData.render();
expect(rendered).toEqual('<powershell>mkdir (Split-Path -Path \'C:/temp/filename.bat\' ) -ea 0\n' +
'Read-S3Object -BucketName \'test\' -key \'filename.bat\' -file \'C:/temp/filename.bat\' -ErrorAction Stop -Region us-east-1\n' +
'mkdir (Split-Path -Path \'c:\\test\\location\\otherScript.bat\' ) -ea 0\n' +
'Read-S3Object -BucketName \'test2\' -key \'filename2.bat\' -file \'c:\\test\\location\\otherScript.bat\' -ErrorAction Stop -Region us-east-1</powershell>',
);

});
test('can windows userdata execute files', () => {
// GIVEN
Expand Down Expand Up @@ -189,6 +218,36 @@ describe('user data', () => {
'aws s3 cp \'s3://test2/filename2.sh\' \'c:\\test\\location\\otherScript.sh\'',
);

});
test('can linux userdata download S3 files from specific region', () => {
// GIVEN
const stack = new Stack();
const userData = ec2.UserData.forLinux();
const bucket = Bucket.fromBucketName( stack, 'testBucket', 'test' );
const bucket2 = Bucket.fromBucketName( stack, 'testBucket2', 'test2' );

// WHEN
userData.addS3DownloadCommand({
bucket,
bucketKey: 'filename.sh',
region: 'us-east-1',
} );
userData.addS3DownloadCommand({
bucket: bucket2,
bucketKey: 'filename2.sh',
localFile: 'c:\\test\\location\\otherScript.sh',
region: 'us-east-1',
} );

// THEN
const rendered = userData.render();
expect(rendered).toEqual('#!/bin/bash\n' +
'mkdir -p $(dirname \'/tmp/filename.sh\')\n' +
'aws s3 cp \'s3://test/filename.sh\' \'/tmp/filename.sh\' --region us-east-1\n' +
'mkdir -p $(dirname \'c:\\test\\location\\otherScript.sh\')\n' +
'aws s3 cp \'s3://test2/filename2.sh\' \'c:\\test\\location\\otherScript.sh\' --region us-east-1',
);

});
test('can linux userdata execute files', () => {
// GIVEN
Expand Down

0 comments on commit 691d377

Please sign in to comment.