|
12 | 12 | ---
|
13 | 13 | <!--END STABILITY BANNER-->
|
14 | 14 |
|
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. |
18 | 16 |
|
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 |
0 commit comments