Skip to main content

Deploy local files and directories to S3

Project description

AWS CDK Assets


Stability: Stable

This is a developer preview (public beta) module. Releases might lack important features and might have future breaking changes.


Assets are local files or directories which are needed by a CDK app. A common example is a directory which contains the handler code for a Lambda function, but assets can represent any artifact that is needed for the app's operation.

When deploying a CDK app that includes constructs with assets, the CDK toolkit will first upload all the assets to S3, and only then deploy the stacks. The S3 locations of the uploaded assets will be passed in as CloudFormation Parameters to the relevant stacks.

The following JavaScript example defines an directory asset which is archived as a .zip file and uploaded to S3 during deployment.

const asset = new assets.Asset(this, 'SampleAsset', {
  path: path.join(__dirname, 'sample-asset-directory')
});

The following JavaScript example defines a file asset, which is uploaded as-is to an S3 bucket during deployment.

const asset = new assets.Asset(this, 'SampleAsset', {
  path: path.join(__dirname, 'file-asset.txt')
});

Attributes

Asset constructs expose the following deploy-time attributes:

In the following example, the various asset attributes are exported as stack outputs:

    const asset = new assets.Asset(this, 'SampleAsset', {
      path: path.join(__dirname, 'sample-asset-directory')
    });

    new cdk.CfnOutput(this, 'S3BucketName', { value: asset.s3BucketName });
    new cdk.CfnOutput(this, 'S3ObjectKey', { value: asset.s3ObjectKey });
    new cdk.CfnOutput(this, 'S3URL', { value: asset.s3Url });

Permissions

IAM roles, users or groups which need to be able to read assets in runtime will should be granted IAM permissions. To do that use the asset.grantRead(principal) method:

The following examples grants an IAM group read permissions on an asset:

const group = new iam.Group(this, 'MyUserGroup');
asset.grantRead(group);

How does it work?

When an asset is defined in a construct, a construct metadata entry aws:cdk:asset is emitted with instructions on where to find the asset and what type of packaging to perform (zip or file). Furthermore, the synthesized CloudFormation template will also include two CloudFormation parameters: one for the asset's bucket and one for the asset S3 key. Those parameters are used to reference the deploy-time values of the asset (using { Ref: "Param" }).

Then, when the stack is deployed, the toolkit will package the asset (i.e. zip the directory), calculate an MD5 hash of the contents and will render an S3 key for this asset within the toolkit's asset store. If the file doesn't exist in the asset store, it is uploaded during deployment.

The toolkit's asset store is an S3 bucket created by the toolkit for each environment the toolkit operates in (environment = account + region).

Now, when the toolkit deploys the stack, it will set the relevant CloudFormation Parameters to point to the actual bucket and key for each asset.

CloudFormation Resource Metadata

NOTE: This section is relevant for authors of AWS Resource Constructs.

In certain situations, it is desirable for tools to be able to know that a certain CloudFormation resource is using a local asset. For example, SAM CLI can be used to invoke AWS Lambda functions locally for debugging purposes.

To enable such use cases, external tools will consult a set of metadata entries on AWS CloudFormation resources:

  • aws:asset:path points to the local path of the asset.
  • aws:asset:property is the name of the resource property where the asset is used

Using these two metadata entries, tools will be able to identify that assets are used by a certain resource, and enable advanced local experiences.

To add these metadata entries to a resource, use the asset.addResourceMetadata(resource, property) method.

See https://github.com/awslabs/aws-cdk/issues/1432 for more details

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws-cdk.aws-s3-assets-0.36.2.tar.gz (41.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aws_cdk.aws_s3_assets-0.36.2-py3-none-any.whl (40.2 kB view details)

Uploaded Python 3

File details

Details for the file aws-cdk.aws-s3-assets-0.36.2.tar.gz.

File metadata

  • Download URL: aws-cdk.aws-s3-assets-0.36.2.tar.gz
  • Upload date:
  • Size: 41.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.5

File hashes

Hashes for aws-cdk.aws-s3-assets-0.36.2.tar.gz
Algorithm Hash digest
SHA256 f057c9ff0409135a2b0d1c13b658157c346d673fc6ebd5dcd5b2e17fc3c61924
MD5 bdc7bd274765537a63fbd7702c0e1c08
BLAKE2b-256 4357658be8611bfb1a5364ecbc0af8c2e4f4b714359007be1bf17e872e446435

See more details on using hashes here.

File details

Details for the file aws_cdk.aws_s3_assets-0.36.2-py3-none-any.whl.

File metadata

  • Download URL: aws_cdk.aws_s3_assets-0.36.2-py3-none-any.whl
  • Upload date:
  • Size: 40.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.5

File hashes

Hashes for aws_cdk.aws_s3_assets-0.36.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3df90b7ebc44b618ac1a8d02fc3d54170cefb40eb55f19caed1002a0cf4bdd77
MD5 761dd0abcb63844b66f911f74f9d1f74
BLAKE2b-256 4be40a629a4839a9ada6974baaf74ee0e4821e35844606183442d393a509eeea

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page