Skip to content

Commit 83eee09

Browse files
author
Elad Ben-Israel
authored
refactor(assets): API cleanups (#2910)
Reorganize asset-related modules as follows: - @aws-cdk/assets: core types related to assets such as filesystem operations and staging - @aws-cdk/aws-s3-assets: file assets deployed to s3 - @aws-cdk/aws-ecr-assets: docker assets deployed to ecr - @aws-cdk/assets-docker: deprecated BREAKING CHANGE: `AssetProps.packaging` has been removed and is now automatically discovered based on the file type. * **assets:** `ZipDirectoryAsset` has been removed, use `aws-s3-assets.Asset`. * **assets:** `FileAsset` has been removed, use `aws-s3-assets.Asset`. * **lambda:** `Code.directory` and `Code.file` have been removed. Use `Code.asset`. * **assets-docker:** The module has been renamed to **aws-ecr-assets**
1 parent 1aa0589 commit 83eee09

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1209
-372
lines changed

packages/@aws-cdk/assets-docker/README.md

+3-54
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,11 @@
33

44
---
55

6-
![Stability: Experimental](https://img.shields.io/badge/stability-Experimental-important.svg?style=for-the-badge)
6+
![Stability: Deprecated](https://img.shields.io/badge/stability-Deprecated-critical.svg?style=for-the-badge)
77

8-
> This API is still under active development and subject to non-backward
9-
> compatible changes or removal in any future version. Use of the API is not recommended in production
10-
> environments. Experimental APIs are not subject to the Semantic Versioning model.
8+
> This API may emit warnings. Backward compatibility is not guaranteed.
119
1210
---
1311
<!--END STABILITY BANNER-->
1412

15-
This module allows bundling Docker images as assets.
16-
17-
Images are built from a local Docker context directory (with a `Dockerfile`),
18-
uploaded to ECR by the CDK toolkit and/or your app's CI-CD pipeline, and can be
19-
naturally referenced in your CDK app.
20-
21-
```typescript
22-
import { DockerImageAsset } from '@aws-cdk/assets-docker';
23-
24-
const asset = new DockerImageAsset(this, 'MyBuildImage', {
25-
directory: path.join(__dirname, 'my-image')
26-
});
27-
```
28-
29-
The directory `my-image` must include a `Dockerfile`.
30-
31-
This will instruct the toolkit to build a Docker image from `my-image`, push it
32-
to an AWS ECR repository and wire the name of the repository as CloudFormation
33-
parameters to your stack.
34-
35-
Use `asset.imageUri` to reference the image (it includes both the ECR image URL
36-
and tag.
37-
38-
You can optionally pass build args to the `docker build` command by specifying
39-
the `buildArgs` property:
40-
41-
```typescript
42-
const asset = new DockerImageAsset(this, 'MyBuildImage', {
43-
directory: path.join(__dirname, 'my-image'),
44-
buildArgs: {
45-
HTTP_PROXY: 'http://10.20.30.2:1234'
46-
}
47-
});
48-
```
49-
50-
### Pull Permissions
51-
52-
Depending on the consumer of your image asset, you will need to make sure
53-
the principal has permissions to pull the image.
54-
55-
In most cases, you should use the `asset.repository.grantPull(principal)`
56-
method. This will modify the IAM policy of the principal to allow it to
57-
pull images from this repository.
58-
59-
If the pulling principal is not in the same account or is an AWS service that
60-
doesn't assume a role in your account (e.g. AWS CodeBuild), pull permissions
61-
must be granted on the __resource policy__ (and not on the principal's policy).
62-
To do that, you can use `asset.repository.addToResourcePolicy(statement)` to
63-
grant the desired principal the following permissions: "ecr:GetDownloadUrlForLayer",
64-
"ecr:BatchGetImage" and "ecr:BatchCheckLayerAvailability".
13+
Deprecated: @aws-cdk/aws-ecr-assets
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export * from './image-asset';
1+
// tslint:disable-next-line:no-console
2+
console.error('the @aws-cdk/assets-docker is deprecated. use @aws-cdk/aws-ecr-assets');

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

+5-31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "@aws-cdk/assets-docker",
33
"version": "0.34.0",
4-
"description": "Docker image assets",
4+
"deprecated": "moved to @aws-cdk/aws-ecr-assets",
5+
"description": "deprecated: moved to @aws-cdk/aws-ecr-assets",
56
"main": "lib/index.js",
67
"types": "lib/index.d.ts",
78
"jsii": {
@@ -57,40 +58,13 @@
5758
},
5859
"license": "Apache-2.0",
5960
"devDependencies": {
60-
"@aws-cdk/assert": "^0.34.0",
61-
"@types/proxyquire": "^1.3.28",
62-
"aws-cdk": "^0.34.0",
6361
"cdk-build-tools": "^0.34.0",
6462
"cdk-integ-tools": "^0.34.0",
65-
"pkglint": "^0.34.0",
66-
"proxyquire": "^2.1.0"
67-
},
68-
"dependencies": {
69-
"@aws-cdk/assets": "^0.34.0",
70-
"@aws-cdk/aws-cloudformation": "^0.34.0",
71-
"@aws-cdk/aws-ecr": "^0.34.0",
72-
"@aws-cdk/aws-iam": "^0.34.0",
73-
"@aws-cdk/aws-lambda": "^0.34.0",
74-
"@aws-cdk/aws-s3": "^0.34.0",
75-
"@aws-cdk/cdk": "^0.34.0",
76-
"@aws-cdk/cx-api": "^0.34.0"
63+
"pkglint": "^0.34.0"
7764
},
7865
"homepage": "https://github.com/awslabs/aws-cdk",
79-
"peerDependencies": {
80-
"@aws-cdk/assets": "^0.34.0",
81-
"@aws-cdk/aws-cloudformation": "^0.34.0",
82-
"@aws-cdk/aws-ecr": "^0.34.0",
83-
"@aws-cdk/aws-iam": "^0.34.0",
84-
"@aws-cdk/aws-lambda": "^0.34.0",
85-
"@aws-cdk/aws-s3": "^0.34.0",
86-
"@aws-cdk/cdk": "^0.34.0",
87-
"@aws-cdk/cx-api": "^0.34.0"
88-
},
89-
"nyc": {
90-
"statements": 70
91-
},
9266
"engines": {
9367
"node": ">= 8.10.0"
9468
},
95-
"stability": "experimental"
96-
}
69+
"stability": "deprecated"
70+
}

packages/@aws-cdk/assets/README.md

+1-80
Original file line numberDiff line numberDiff line change
@@ -12,84 +12,5 @@
1212
---
1313
<!--END STABILITY BANNER-->
1414

15-
Assets are local files or directories which are needed by a CDK app. A common
16-
example is a directory which contains the handler code for a Lambda function,
17-
but assets can represent any artifact that is needed for the app's operation.
15+
This module includes core classes related to CDK assets.
1816

19-
When deploying a CDK app that includes constructs with assets, the CDK toolkit
20-
will first upload all the assets to S3, and only then deploy the stacks. The S3
21-
locations of the uploaded assets will be passed in as CloudFormation Parameters
22-
to the relevant stacks.
23-
24-
The following JavaScript example defines an directory asset which is archived as
25-
a .zip file and uploaded to S3 during deployment.
26-
27-
[Example of a ZipDirectoryAsset](./test/integ.assets.directory.lit.ts)
28-
29-
The following JavaScript example defines a file asset, which is uploaded as-is
30-
to an S3 bucket during deployment.
31-
32-
[Example of a FileAsset](./test/integ.assets.file.lit.ts)
33-
34-
## Attributes
35-
36-
`Asset` constructs expose the following deploy-time attributes:
37-
38-
* `s3BucketName` - the name of the assets S3 bucket.
39-
* `s3ObjectKey` - the S3 object key of the asset file (whether it's a file or a zip archive)
40-
* `s3Url` - the S3 URL of the asset (i.e. https://s3.us-east-1.amazonaws.com/mybucket/mykey.zip)
41-
42-
In the following example, the various asset attributes are exported as stack outputs:
43-
44-
[Example of referencing an asset](./test/integ.assets.refs.lit.ts)
45-
46-
## Permissions
47-
48-
IAM roles, users or groups which need to be able to read assets in runtime will should be
49-
granted IAM permissions. To do that use the `asset.grantRead(principal)` method:
50-
51-
The following examples grants an IAM group read permissions on an asset:
52-
53-
[Example of granting read access to an asset](./test/integ.assets.permissions.lit.ts)
54-
55-
## How does it work?
56-
57-
When an asset is defined in a construct, a construct metadata entry
58-
`aws:cdk:asset` is emitted with instructions on where to find the asset and what
59-
type of packaging to perform (`zip` or `file`). Furthermore, the synthesized
60-
CloudFormation template will also include two CloudFormation parameters: one for
61-
the asset's bucket and one for the asset S3 key. Those parameters are used to
62-
reference the deploy-time values of the asset (using `{ Ref: "Param" }`).
63-
64-
Then, when the stack is deployed, the toolkit will package the asset (i.e. zip
65-
the directory), calculate an MD5 hash of the contents and will render an S3 key
66-
for this asset within the toolkit's asset store. If the file doesn't exist in
67-
the asset store, it is uploaded during deployment.
68-
69-
> The toolkit's asset store is an S3 bucket created by the toolkit for each
70-
environment the toolkit operates in (environment = account + region).
71-
72-
Now, when the toolkit deploys the stack, it will set the relevant CloudFormation
73-
Parameters to point to the actual bucket and key for each asset.
74-
75-
## CloudFormation Resource Metadata
76-
77-
> NOTE: This section is relevant for authors of AWS Resource Constructs.
78-
79-
In certain situations, it is desirable for tools to be able to know that a certain CloudFormation
80-
resource is using a local asset. For example, SAM CLI can be used to invoke AWS Lambda functions
81-
locally for debugging purposes.
82-
83-
To enable such use cases, external tools will consult a set of metadata entries on AWS CloudFormation
84-
resources:
85-
86-
- `aws:asset:path` points to the local path of the asset.
87-
- `aws:asset:property` is the name of the resource property where the asset is used
88-
89-
Using these two metadata entries, tools will be able to identify that assets are used
90-
by a certain resource, and enable advanced local experiences.
91-
92-
To add these metadata entries to a resource, use the
93-
`asset.addResourceMetadata(resource, property)` method.
94-
95-
See https://github.com/awslabs/aws-cdk/issues/1432 for more details

packages/@aws-cdk/assets/lib/api.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Common interface for all assets.
3+
*/
4+
export interface IAsset {
5+
/**
6+
* A hash of the source of this asset, which is available at construction time. As this is a plain
7+
* string, it can be used in construct IDs in order to enforce creation of a new resource when
8+
* the content hash has changed.
9+
*/
10+
readonly sourceHash: string;
11+
12+
/**
13+
* A hash of the bundle for of this asset, which is only available at deployment time. As this is
14+
* a late-bound token, it may not be used in construct IDs, but can be passed as a resource
15+
* property in order to force a change on a resource when an asset is effectively updated. This is
16+
* more reliable than `sourceHash` in particular for assets which bundling phase involve external
17+
* resources that can change over time (such as Docker image builds).
18+
*/
19+
readonly artifactHash: string;
20+
}

packages/@aws-cdk/assets/lib/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from './asset';
21
export * from './fs/copy-options';
32
export * from './fs/follow-mode';
43
export * from './staging';
4+
export * from './api';

packages/@aws-cdk/assets/lib/staging.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface StagingProps extends CopyOptions {
1212
* Stages a file or directory from a location on the file system into a staging
1313
* directory.
1414
*
15-
* This is controlled by the context key 'aws:cdk:asset-staging-dir' and enabled
15+
* This is controlled by the context key 'aws:cdk:asset-staging' and enabled
1616
* by the CLI by default in order to ensure that when the CDK app exists, all
1717
* assets are available for deployment. Otherwise, if an app references assets
1818
* in temporary locations, those will not be available when it exists (see

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

-4
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,12 @@
7272
"ts-mock-imports": "^1.2.3"
7373
},
7474
"dependencies": {
75-
"@aws-cdk/aws-iam": "^0.34.0",
76-
"@aws-cdk/aws-s3": "^0.34.0",
7775
"@aws-cdk/cdk": "^0.34.0",
7876
"@aws-cdk/cx-api": "^0.34.0",
7977
"minimatch": "^3.0.4"
8078
},
8179
"homepage": "https://github.com/awslabs/aws-cdk",
8280
"peerDependencies": {
83-
"@aws-cdk/aws-iam": "^0.34.0",
84-
"@aws-cdk/aws-s3": "^0.34.0",
8581
"@aws-cdk/cdk": "^0.34.0",
8682
"@aws-cdk/cx-api": "^0.34.0"
8783
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { App, Stack } from '@aws-cdk/cdk';
2+
import cxapi = require('@aws-cdk/cx-api');
3+
import fs = require('fs');
4+
import { Test } from 'nodeunit';
5+
import path = require('path');
6+
import { Staging } from '../lib';
7+
8+
export = {
9+
'base case'(test: Test) {
10+
// GIVEN
11+
const stack = new Stack();
12+
const sourcePath = path.join(__dirname, 'fs', 'fixtures', 'test1');
13+
14+
// WHEN
15+
const staging = new Staging(stack, 's1', { sourcePath });
16+
17+
test.deepEqual(staging.sourceHash, '2f37f937c51e2c191af66acf9b09f548926008ec68c575bd2ee54b6e997c0e00');
18+
test.deepEqual(staging.sourcePath, sourcePath);
19+
test.deepEqual(stack.resolve(staging.stagedPath), 'asset.2f37f937c51e2c191af66acf9b09f548926008ec68c575bd2ee54b6e997c0e00');
20+
test.done();
21+
},
22+
23+
'staging can be disabled through context'(test: Test) {
24+
// GIVEN
25+
const stack = new Stack();
26+
stack.node.setContext(cxapi.DISABLE_ASSET_STAGING_CONTEXT, true);
27+
const sourcePath = path.join(__dirname, 'fs', 'fixtures', 'test1');
28+
29+
// WHEN
30+
const staging = new Staging(stack, 's1', { sourcePath });
31+
32+
test.deepEqual(staging.sourceHash, '2f37f937c51e2c191af66acf9b09f548926008ec68c575bd2ee54b6e997c0e00');
33+
test.deepEqual(staging.sourcePath, sourcePath);
34+
test.deepEqual(stack.resolve(staging.stagedPath), sourcePath);
35+
test.done();
36+
},
37+
38+
'files are copied to the output directory during synth'(test: Test) {
39+
// GIVEN
40+
const app = new App();
41+
const stack = new Stack(app, 'stack');
42+
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');
43+
const file = path.join(__dirname, 'fs', 'fixtures.tar.gz');
44+
45+
// WHEN
46+
new Staging(stack, 's1', { sourcePath: directory });
47+
new Staging(stack, 'file', { sourcePath: file });
48+
49+
// THEN
50+
const assembly = app.synth();
51+
test.deepEqual(fs.readdirSync(assembly.directory), [
52+
'asset.2f37f937c51e2c191af66acf9b09f548926008ec68c575bd2ee54b6e997c0e00',
53+
'asset.af10ac04b3b607b0f8659c8f0cee8c343025ee75baf0b146f10f0e5311d2c46b.gz',
54+
'cdk.out',
55+
'manifest.json',
56+
'stack.template.json'
57+
]);
58+
test.done();
59+
}
60+
};

packages/@aws-cdk/aws-codebuild/lib/project.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { DockerImageAsset, DockerImageAssetProps } from '@aws-cdk/assets-docker';
21
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
32
import ec2 = require('@aws-cdk/aws-ec2');
43
import ecr = require('@aws-cdk/aws-ecr');
4+
import { DockerImageAsset, DockerImageAssetProps } from '@aws-cdk/aws-ecr-assets';
55
import events = require('@aws-cdk/aws-events');
66
import iam = require('@aws-cdk/aws-iam');
77
import kms = require('@aws-cdk/aws-kms');

packages/@aws-cdk/aws-codebuild/package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@
7777
"pkglint": "^0.34.0"
7878
},
7979
"dependencies": {
80+
"@aws-cdk/aws-s3-assets": "^0.34.0",
8081
"@aws-cdk/assets": "^0.34.0",
81-
"@aws-cdk/assets-docker": "^0.34.0",
82+
"@aws-cdk/aws-ecr-assets": "^0.34.0",
8283
"@aws-cdk/aws-cloudwatch": "^0.34.0",
8384
"@aws-cdk/aws-codecommit": "^0.34.0",
8485
"@aws-cdk/aws-ec2": "^0.34.0",
@@ -91,8 +92,9 @@
9192
},
9293
"homepage": "https://github.com/awslabs/aws-cdk",
9394
"peerDependencies": {
95+
"@aws-cdk/aws-s3-assets": "^0.34.0",
9496
"@aws-cdk/assets": "^0.34.0",
95-
"@aws-cdk/assets-docker": "^0.34.0",
97+
"@aws-cdk/aws-ecr-assets": "^0.34.0",
9698
"@aws-cdk/aws-cloudwatch": "^0.34.0",
9799
"@aws-cdk/aws-codecommit": "^0.34.0",
98100
"@aws-cdk/aws-ec2": "^0.34.0",
@@ -112,4 +114,4 @@
112114
]
113115
},
114116
"stability": "experimental"
115-
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.js
2+
*.js.map
3+
*.d.ts
4+
node_modules
5+
dist
6+
tsconfig.json
7+
tslint.json
8+
9+
.LAST_BUILD
10+
.nyc_output
11+
coverage
12+
13+
.jsii
14+
15+
.nycrc
16+
.LAST_PACKAGE
17+
*.snk

0 commit comments

Comments
 (0)