Skip to content

Commit c2296a8

Browse files
authored
fix(ecr-assets): fix repeated deploys of stacks with tar assets (#23497)
The current implementation fails because it assumes the output of `docker load` can only be ``` Loaded Image: {image} ``` However, if docker has to take any actions (such as untagging a previous image) the output will look like ``` {error message} {error message} Loaded Image: {image} ``` To fix this, we can simply take the last line of the output via `tail` before we attempt to extract the image. If the last line isn't of the correct form, this will fail in an effectively equivalent way to how it failed previously. A previous attempt at fixing this (#18823) used a more complicated `sed` command which used a cli flag that is not consistently available. This approach won't work on all platforms (ex. macos) and is also much less clear that it's correct. This change also adds an integration test that tests the tarbell stack, though it doesn't test the repeated deploy automatically. I have tested that by running the integration test multiple times with an image modification in between. We also vendor the tarball of the hello-world docker image, which results in ~20kb of additional data in the repo. This is about as small as we can get a vendored image with as straightforward a vendoring process. fixes #18822 ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Construct Runtime Dependencies: * [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 4b08e20 commit c2296a8

14 files changed

+631
-2
lines changed

packages/@aws-cdk/aws-ecr-assets/lib/tarball-asset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class TarballImageAsset extends Construct implements IAsset {
7777
executable: [
7878
'sh',
7979
'-c',
80-
`docker load -i ${relativePathInOutDir} | sed "s/Loaded image: //g"`,
80+
`docker load -i ${relativePathInOutDir} | tail -n 1 | sed "s/Loaded image: //g"`,
8181
],
8282
});
8383

packages/@aws-cdk/aws-ecr-assets/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"@aws-cdk/assertions": "0.0.0",
7777
"@aws-cdk/cdk-build-tools": "0.0.0",
7878
"@aws-cdk/integ-runner": "0.0.0",
79+
"@aws-cdk/integ-tests": "0.0.0",
7980
"@aws-cdk/cloud-assembly-schema": "0.0.0",
8081
"@aws-cdk/pkglint": "0.0.0",
8182
"@types/jest": "^27.5.2",
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "22.0.0",
3+
"files": {
4+
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
5+
"source": {
6+
"path": "LoadFromTarballDefaultTestDeployAssert46673615.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"Parameters": {
3+
"BootstrapVersion": {
4+
"Type": "AWS::SSM::Parameter::Value<String>",
5+
"Default": "/cdk-bootstrap/hnb659fds/version",
6+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
7+
}
8+
},
9+
"Rules": {
10+
"CheckBootstrapVersion": {
11+
"Assertions": [
12+
{
13+
"Assert": {
14+
"Fn::Not": [
15+
{
16+
"Fn::Contains": [
17+
[
18+
"1",
19+
"2",
20+
"3",
21+
"4",
22+
"5"
23+
],
24+
{
25+
"Ref": "BootstrapVersion"
26+
}
27+
]
28+
}
29+
]
30+
},
31+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
32+
}
33+
]
34+
}
35+
}
36+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":"22.0.0"}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"version": "22.0.0",
3+
"files": {
4+
"7e85363095650cdc9e77b849e3afda82da3123dd43f5d237b9601f816ddf583e": {
5+
"source": {
6+
"path": "integ-assets-tarball.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "7e85363095650cdc9e77b849e3afda82da3123dd43f5d237b9601f816ddf583e.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {
19+
"a5fbe3aa4a0e49fac7d3fbe7b913cf022d307bfad6ef1e88e06cc2a229523474": {
20+
"source": {
21+
"executable": [
22+
"sh",
23+
"-c",
24+
"docker load -i asset.a5fbe3aa4a0e49fac7d3fbe7b913cf022d307bfad6ef1e88e06cc2a229523474.tar | tail -n 1 | sed \"s/Loaded image: //g\""
25+
]
26+
},
27+
"destinations": {
28+
"current_account-current_region": {
29+
"repositoryName": "cdk-hnb659fds-container-assets-${AWS::AccountId}-${AWS::Region}",
30+
"imageTag": "a5fbe3aa4a0e49fac7d3fbe7b913cf022d307bfad6ef1e88e06cc2a229523474",
31+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-image-publishing-role-${AWS::AccountId}-${AWS::Region}"
32+
}
33+
}
34+
}
35+
}
36+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
{
2+
"Resources": {
3+
"MyUserDC45028B": {
4+
"Type": "AWS::IAM::User"
5+
},
6+
"MyUserDefaultPolicy7B897426": {
7+
"Type": "AWS::IAM::Policy",
8+
"Properties": {
9+
"PolicyDocument": {
10+
"Statement": [
11+
{
12+
"Action": [
13+
"ecr:BatchCheckLayerAvailability",
14+
"ecr:BatchGetImage",
15+
"ecr:GetDownloadUrlForLayer"
16+
],
17+
"Effect": "Allow",
18+
"Resource": {
19+
"Fn::Join": [
20+
"",
21+
[
22+
"arn:",
23+
{
24+
"Ref": "AWS::Partition"
25+
},
26+
":ecr:",
27+
{
28+
"Ref": "AWS::Region"
29+
},
30+
":",
31+
{
32+
"Ref": "AWS::AccountId"
33+
},
34+
":repository/",
35+
{
36+
"Fn::Sub": "cdk-hnb659fds-container-assets-${AWS::AccountId}-${AWS::Region}"
37+
}
38+
]
39+
]
40+
}
41+
},
42+
{
43+
"Action": "ecr:GetAuthorizationToken",
44+
"Effect": "Allow",
45+
"Resource": "*"
46+
}
47+
],
48+
"Version": "2012-10-17"
49+
},
50+
"PolicyName": "MyUserDefaultPolicy7B897426",
51+
"Users": [
52+
{
53+
"Ref": "MyUserDC45028B"
54+
}
55+
]
56+
}
57+
}
58+
},
59+
"Outputs": {
60+
"ImageUri": {
61+
"Value": {
62+
"Fn::Sub": "${AWS::AccountId}.dkr.ecr.${AWS::Region}.${AWS::URLSuffix}/cdk-hnb659fds-container-assets-${AWS::AccountId}-${AWS::Region}:a5fbe3aa4a0e49fac7d3fbe7b913cf022d307bfad6ef1e88e06cc2a229523474"
63+
}
64+
},
65+
"ImageUri2": {
66+
"Value": {
67+
"Fn::Sub": "${AWS::AccountId}.dkr.ecr.${AWS::Region}.${AWS::URLSuffix}/cdk-hnb659fds-container-assets-${AWS::AccountId}-${AWS::Region}:a5fbe3aa4a0e49fac7d3fbe7b913cf022d307bfad6ef1e88e06cc2a229523474"
68+
}
69+
}
70+
},
71+
"Parameters": {
72+
"BootstrapVersion": {
73+
"Type": "AWS::SSM::Parameter::Value<String>",
74+
"Default": "/cdk-bootstrap/hnb659fds/version",
75+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
76+
}
77+
},
78+
"Rules": {
79+
"CheckBootstrapVersion": {
80+
"Assertions": [
81+
{
82+
"Assert": {
83+
"Fn::Not": [
84+
{
85+
"Fn::Contains": [
86+
[
87+
"1",
88+
"2",
89+
"3",
90+
"4",
91+
"5"
92+
],
93+
{
94+
"Ref": "BootstrapVersion"
95+
}
96+
]
97+
}
98+
]
99+
},
100+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
101+
}
102+
]
103+
}
104+
}
105+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "22.0.0",
3+
"testCases": {
4+
"LoadFromTarball/DefaultTest": {
5+
"stacks": [
6+
"integ-assets-tarball"
7+
],
8+
"assertionStack": "LoadFromTarball/DefaultTest/DeployAssert",
9+
"assertionStackName": "LoadFromTarballDefaultTestDeployAssert46673615"
10+
}
11+
}
12+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
{
2+
"version": "22.0.0",
3+
"artifacts": {
4+
"integ-assets-tarball.assets": {
5+
"type": "cdk:asset-manifest",
6+
"properties": {
7+
"file": "integ-assets-tarball.assets.json",
8+
"requiresBootstrapStackVersion": 6,
9+
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version"
10+
}
11+
},
12+
"integ-assets-tarball": {
13+
"type": "aws:cloudformation:stack",
14+
"environment": "aws://unknown-account/unknown-region",
15+
"properties": {
16+
"templateFile": "integ-assets-tarball.template.json",
17+
"validateOnSynth": false,
18+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
19+
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
20+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/7e85363095650cdc9e77b849e3afda82da3123dd43f5d237b9601f816ddf583e.json",
21+
"requiresBootstrapStackVersion": 6,
22+
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
23+
"additionalDependencies": [
24+
"integ-assets-tarball.assets"
25+
],
26+
"lookupRole": {
27+
"arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}",
28+
"requiresBootstrapStackVersion": 8,
29+
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version"
30+
}
31+
},
32+
"dependencies": [
33+
"integ-assets-tarball.assets"
34+
],
35+
"metadata": {
36+
"/integ-assets-tarball/MyUser/Resource": [
37+
{
38+
"type": "aws:cdk:logicalId",
39+
"data": "MyUserDC45028B"
40+
}
41+
],
42+
"/integ-assets-tarball/MyUser/DefaultPolicy/Resource": [
43+
{
44+
"type": "aws:cdk:logicalId",
45+
"data": "MyUserDefaultPolicy7B897426"
46+
}
47+
],
48+
"/integ-assets-tarball/ImageUri": [
49+
{
50+
"type": "aws:cdk:logicalId",
51+
"data": "ImageUri"
52+
}
53+
],
54+
"/integ-assets-tarball/ImageUri2": [
55+
{
56+
"type": "aws:cdk:logicalId",
57+
"data": "ImageUri2"
58+
}
59+
],
60+
"/integ-assets-tarball/BootstrapVersion": [
61+
{
62+
"type": "aws:cdk:logicalId",
63+
"data": "BootstrapVersion"
64+
}
65+
],
66+
"/integ-assets-tarball/CheckBootstrapVersion": [
67+
{
68+
"type": "aws:cdk:logicalId",
69+
"data": "CheckBootstrapVersion"
70+
}
71+
]
72+
},
73+
"displayName": "integ-assets-tarball"
74+
},
75+
"LoadFromTarballDefaultTestDeployAssert46673615.assets": {
76+
"type": "cdk:asset-manifest",
77+
"properties": {
78+
"file": "LoadFromTarballDefaultTestDeployAssert46673615.assets.json",
79+
"requiresBootstrapStackVersion": 6,
80+
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version"
81+
}
82+
},
83+
"LoadFromTarballDefaultTestDeployAssert46673615": {
84+
"type": "aws:cloudformation:stack",
85+
"environment": "aws://unknown-account/unknown-region",
86+
"properties": {
87+
"templateFile": "LoadFromTarballDefaultTestDeployAssert46673615.template.json",
88+
"validateOnSynth": false,
89+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
90+
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
91+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
92+
"requiresBootstrapStackVersion": 6,
93+
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
94+
"additionalDependencies": [
95+
"LoadFromTarballDefaultTestDeployAssert46673615.assets"
96+
],
97+
"lookupRole": {
98+
"arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}",
99+
"requiresBootstrapStackVersion": 8,
100+
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version"
101+
}
102+
},
103+
"dependencies": [
104+
"LoadFromTarballDefaultTestDeployAssert46673615.assets"
105+
],
106+
"metadata": {
107+
"/LoadFromTarball/DefaultTest/DeployAssert/BootstrapVersion": [
108+
{
109+
"type": "aws:cdk:logicalId",
110+
"data": "BootstrapVersion"
111+
}
112+
],
113+
"/LoadFromTarball/DefaultTest/DeployAssert/CheckBootstrapVersion": [
114+
{
115+
"type": "aws:cdk:logicalId",
116+
"data": "CheckBootstrapVersion"
117+
}
118+
]
119+
},
120+
"displayName": "LoadFromTarball/DefaultTest/DeployAssert"
121+
},
122+
"Tree": {
123+
"type": "cdk:tree",
124+
"properties": {
125+
"file": "tree.json"
126+
}
127+
}
128+
}
129+
}

0 commit comments

Comments
 (0)