Skip to content

Commit 14c21b1

Browse files
fix(core): File copy mechanism broken (#591)
* Fixing file copy to have latest file from both s3 and repo - Performing s3 deployment using aws sync to get latest from repo - After bucket deployment s3copyfiles to get latest file from customer s3 bucket. * Removing time check for copy to copy everytime when we find in bucket
1 parent 763ccee commit 14c21b1

File tree

3 files changed

+57
-72
lines changed

3 files changed

+57
-72
lines changed

src/deployments/cdk/src/deployments/artifacts/step-1.ts

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { AcceleratorConfig } from '@aws-accelerator/common-config/src';
55
import { AccountStacks, AccountStack } from '../../common/account-stacks';
66
import { JsonOutputValue } from '../../common/json-output';
77
import { ArtifactName, CfnArtifactOutput } from './outputs';
8+
import { createRoleName } from '@aws-accelerator/cdk-accelerator/src/core/accelerator-name-generator';
9+
import { S3CopyFiles } from '@aws-accelerator/custom-resource-s3-copy-files';
810

911
export interface ArtifactsStep1Props {
1012
accountStacks: AccountStacks;
@@ -18,8 +20,14 @@ export async function step1(props: ArtifactsStep1Props) {
1820
const masterAccountKey = config.getMandatoryAccountKey('master');
1921
const masterAccountStack = accountStacks.getOrCreateAccountStack(masterAccountKey);
2022

23+
// Get the location of the original central bucket
24+
const centralConfigBucketName = config['global-options']['central-bucket'];
25+
const centralConfigBucket = s3.Bucket.fromBucketAttributes(masterAccountStack, 'CentralBucket', {
26+
bucketName: centralConfigBucketName,
27+
});
28+
2129
// upload SCP Artifacts
22-
uploadArtifacts({
30+
const scpUpload = uploadArtifacts({
2331
accountStack: masterAccountStack,
2432
artifactName: 'SCP',
2533
artifactFolderName: 'SCPs',
@@ -29,7 +37,7 @@ export async function step1(props: ArtifactsStep1Props) {
2937
});
3038

3139
// upload IAM-Policies Artifacts
32-
uploadArtifacts({
40+
const iamUpload = uploadArtifacts({
3341
accountStack: masterAccountStack,
3442
artifactName: 'IamPolicy',
3543
artifactFolderName: 'iam-policies',
@@ -39,7 +47,7 @@ export async function step1(props: ArtifactsStep1Props) {
3947
});
4048

4149
// upload RDGW Artifacts
42-
uploadArtifacts({
50+
const rdgwUpload = uploadArtifacts({
4351
accountStack: masterAccountStack,
4452
artifactName: 'Rdgw',
4553
artifactFolderName: 'scripts',
@@ -49,7 +57,7 @@ export async function step1(props: ArtifactsStep1Props) {
4957
});
5058

5159
// upload Rsyslog Artifacts
52-
uploadArtifacts({
60+
const rsyslogUpload = uploadArtifacts({
5361
accountStack: masterAccountStack,
5462
artifactName: 'Rsyslog',
5563
artifactFolderName: 'rsyslog',
@@ -59,7 +67,7 @@ export async function step1(props: ArtifactsStep1Props) {
5967
});
6068

6169
// upload SSM-Document Artifacts
62-
uploadArtifacts({
70+
const ssmUpload = uploadArtifacts({
6371
accountStack: masterAccountStack,
6472
artifactName: 'SsmDocument',
6573
artifactFolderName: 'ssm-documents',
@@ -68,6 +76,21 @@ export async function step1(props: ArtifactsStep1Props) {
6876
destinationKeyPrefix: 'ssm-documents',
6977
keepExistingFiles: true,
7078
});
79+
80+
// Copy files from source to destination
81+
const copyFiles = new S3CopyFiles(masterAccountStack, 'CopyFiles', {
82+
roleName: createRoleName('S3CopyFiles'),
83+
sourceBucket: centralConfigBucket,
84+
destinationBucket: centralBucket,
85+
deleteSourceObjects: false,
86+
deleteSourceBucket: false,
87+
forceUpdate: true,
88+
});
89+
copyFiles.node.addDependency(ssmUpload);
90+
copyFiles.node.addDependency(rsyslogUpload);
91+
copyFiles.node.addDependency(rdgwUpload);
92+
copyFiles.node.addDependency(iamUpload);
93+
copyFiles.node.addDependency(scpUpload);
7194
}
7295

7396
function uploadArtifacts(props: {
@@ -78,7 +101,7 @@ function uploadArtifacts(props: {
78101
centralBucket: s3.IBucket;
79102
destinationKeyPrefix?: string;
80103
keepExistingFiles?: boolean;
81-
}) {
104+
}): s3deployment.BucketDeployment {
82105
const {
83106
accountStack,
84107
artifactName,
@@ -102,15 +125,16 @@ function uploadArtifacts(props: {
102125
artifactFolderName,
103126
);
104127

105-
// TODO Leave existing files in the folder
106-
// TODO Do not override existing files
107-
// See https://github.com/aws/aws-cdk/issues/953
108-
new s3deployment.BucketDeployment(accountStack, `${artifactName}ArtifactsDeployment${accountKey}`, {
109-
sources: [s3deployment.Source.asset(artifactsFolderPath)],
110-
destinationBucket: centralBucket,
111-
destinationKeyPrefix,
112-
prune: !keepExistingFiles,
113-
});
128+
const s3Deployment = new s3deployment.BucketDeployment(
129+
accountStack,
130+
`${artifactName}ArtifactsDeployment${accountKey}`,
131+
{
132+
sources: [s3deployment.Source.asset(artifactsFolderPath)],
133+
destinationBucket: centralBucket,
134+
destinationKeyPrefix,
135+
prune: !keepExistingFiles,
136+
},
137+
);
114138

115139
// outputs to store reference artifacts s3 bucket information
116140
new JsonOutputValue(accountStack, `${artifactName}ArtifactsOutput${accountKey}`, {
@@ -130,4 +154,5 @@ function uploadArtifacts(props: {
130154
bucketName: centralBucket.bucketName,
131155
keyPrefix: artifactKeyPrefix,
132156
});
157+
return s3Deployment;
133158
}

src/deployments/cdk/src/deployments/defaults/step-1.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,6 @@ function createCentralBucketCopy(props: DefaultsStep1Props) {
7979
const masterAccountConfig = config['global-options']['aws-org-master'];
8080
const masterAccountStack = accountStacks.getOrCreateAccountStack(masterAccountConfig.account);
8181

82-
// Get the location of the original central bucket
83-
const centralBucketName = config['global-options']['central-bucket'];
84-
const centralBucket = s3.Bucket.fromBucketAttributes(masterAccountStack, 'CentralBucket', {
85-
bucketName: centralBucketName,
86-
});
87-
8882
const keyAlias = createEncryptionKeyName('Config-Key');
8983
const encryptionKey = new kms.Key(masterAccountStack, 'CentralBucketKey', {
9084
alias: `alias/${keyAlias}`,
@@ -131,17 +125,6 @@ function createCentralBucketCopy(props: DefaultsStep1Props) {
131125
}),
132126
);
133127

134-
// Copy files from source to destination
135-
const copyFiles = new S3CopyFiles(masterAccountStack, 'CopyFiles', {
136-
roleName: createRoleName('S3CopyFiles'),
137-
sourceBucket: centralBucket,
138-
destinationBucket: bucket,
139-
deleteSourceObjects: false,
140-
deleteSourceBucket: false,
141-
forceUpdate: true,
142-
});
143-
copyFiles.node.addDependency(bucket);
144-
145128
new CfnCentralBucketOutput(masterAccountStack, 'CentralBucketOutput', {
146129
bucketArn: bucket.bucketArn,
147130
bucketName: bucket.bucketName,

src/lib/custom-resources/cdk-s3-copy-files/runtime/src/index.ts

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -132,55 +132,32 @@ async function copyObject(props: {
132132
const { sourceBucketName, destinationBucketName, deleteSourceObjects, sourceObject } = props;
133133
const sourceKey = sourceObject.Key!;
134134

135-
let destinationLastModified;
135+
let object: AWS.S3.GetObjectOutput;
136136
try {
137-
const headObject = await throttlingBackOff(() =>
137+
object = await throttlingBackOff(() =>
138138
s3
139-
.headObject({
140-
Bucket: destinationBucketName,
139+
.getObject({
140+
Bucket: sourceBucketName,
141141
Key: sourceKey,
142142
})
143143
.promise(),
144144
);
145-
destinationLastModified = headObject.LastModified;
146145
} catch (e) {
147-
console.debug(`Unable to head S3 object s3://${destinationBucketName}/${sourceKey}: ${e}`);
146+
throw new Error(`Unable to get S3 object s3://${sourceBucketName}/${sourceKey}: ${e}`);
148147
}
149148

150-
if (
151-
!destinationLastModified ||
152-
!sourceObject.LastModified ||
153-
compareDate(destinationLastModified, sourceObject.LastModified) < 0
154-
) {
155-
let object: AWS.S3.GetObjectOutput;
156-
try {
157-
object = await throttlingBackOff(() =>
158-
s3
159-
.getObject({
160-
Bucket: sourceBucketName,
161-
Key: sourceKey,
162-
})
163-
.promise(),
164-
);
165-
} catch (e) {
166-
throw new Error(`Unable to get S3 object s3://${sourceBucketName}/${sourceKey}: ${e}`);
167-
}
168-
169-
try {
170-
await throttlingBackOff(() =>
171-
s3
172-
.putObject({
173-
Bucket: destinationBucketName,
174-
Key: sourceKey,
175-
Body: object.Body,
176-
})
177-
.promise(),
178-
);
179-
} catch (e) {
180-
throw new Error(`Unable to put S3 object s3://${destinationBucketName}/${sourceKey}: ${e}`);
181-
}
182-
} else {
183-
console.debug(`Skipping copy of s3://${sourceBucketName}/${sourceKey}`);
149+
try {
150+
await throttlingBackOff(() =>
151+
s3
152+
.putObject({
153+
Bucket: destinationBucketName,
154+
Key: sourceKey,
155+
Body: object.Body,
156+
})
157+
.promise(),
158+
);
159+
} catch (e) {
160+
throw new Error(`Unable to put S3 object s3://${destinationBucketName}/${sourceKey}: ${e}`);
184161
}
185162

186163
if (deleteSourceObjects) {

0 commit comments

Comments
 (0)