What is @aws-cdk/aws-s3-assets?
@aws-cdk/aws-s3-assets is an AWS CDK library that allows you to manage and deploy assets to Amazon S3. It simplifies the process of uploading files and directories to S3 and integrates seamlessly with other AWS CDK constructs.
What are @aws-cdk/aws-s3-assets's main functionalities?
Upload a File to S3
This feature allows you to upload a single file to an S3 bucket. The code sample demonstrates how to create a new asset from a file located at 'path/to/file.txt' and upload it to S3.
const s3assets = require('@aws-cdk/aws-s3-assets');
const cdk = require('@aws-cdk/core');
class MyStack extends cdk.Stack {
constructor(scope, id, props) {
super(scope, id, props);
new s3assets.Asset(this, 'MyAsset', {
path: 'path/to/file.txt'
});
}
}
const app = new cdk.App();
new MyStack(app, 'MyStack');
Upload a Directory to S3
This feature allows you to upload an entire directory to an S3 bucket. The code sample demonstrates how to create a new asset from a directory located at 'path/to/directory' and upload it to S3.
const s3assets = require('@aws-cdk/aws-s3-assets');
const cdk = require('@aws-cdk/core');
class MyStack extends cdk.Stack {
constructor(scope, id, props) {
super(scope, id, props);
new s3assets.Asset(this, 'MyAsset', {
path: 'path/to/directory'
});
}
}
const app = new cdk.App();
new MyStack(app, 'MyStack');
Grant Read Permissions
This feature allows you to grant read permissions to an IAM role for the uploaded asset. The code sample demonstrates how to create a new asset, an IAM role, and grant read permissions to the role for the asset.
const s3assets = require('@aws-cdk/aws-s3-assets');
const cdk = require('@aws-cdk/core');
const iam = require('@aws-cdk/aws-iam');
class MyStack extends cdk.Stack {
constructor(scope, id, props) {
super(scope, id, props);
const asset = new s3assets.Asset(this, 'MyAsset', {
path: 'path/to/file.txt'
});
const role = new iam.Role(this, 'MyRole', {
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com')
});
asset.grantRead(role);
}
}
const app = new cdk.App();
new MyStack(app, 'MyStack');
Other packages similar to @aws-cdk/aws-s3-assets
aws-sdk
The aws-sdk package is the official AWS SDK for JavaScript. It provides a comprehensive set of tools for interacting with AWS services, including S3. Unlike @aws-cdk/aws-s3-assets, which is focused on asset management within the AWS CDK framework, aws-sdk offers a broader range of functionalities for direct API interactions with AWS services.
s3-upload-stream
The s3-upload-stream package is a Node.js module that allows you to stream data directly to S3. It is useful for handling large files or data streams. While @aws-cdk/aws-s3-assets is designed for use within the AWS CDK framework and simplifies asset management, s3-upload-stream provides more granular control over the upload process.
s3-sync-client
The s3-sync-client package is a high-level client for synchronizing local files and directories with S3. It offers similar functionality to @aws-cdk/aws-s3-assets in terms of uploading files and directories, but it is not tied to the AWS CDK framework and can be used independently in any Node.js application.
AWS CDK Assets
This is a developer preview (public beta) module. Releases might lack important features and might have
future breaking changes.
This API is still under active development and subject to non-backward
compatible changes or removal in any future version. Use of the API is not recommended in production
environments. Experimental APIs are not subject to the Semantic Versioning model.
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.
Example of a ZipDirectoryAsset
The following JavaScript example defines a file asset, which is uploaded as-is
to an S3 bucket during deployment.
Example of a FileAsset
Attributes
Asset
constructs expose the following deploy-time attributes:
In the following example, the various asset attributes are exported as stack outputs:
Example of referencing an asset
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:
Example of granting read access to an asset
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/aws/aws-cdk/issues/1432 for more details
1.18.0 (2019-11-25)
General Availability of AWS CDK for .NET and Java!! ๐๐๐ฅ๐ฅ๐พ๐พ
We are excited to announce the general availability of support for the .NET family of languages (C#,
F#, ...) as well as Java!
We want to express our gratitude to all of our early customers as well as the amazing contributors
for all the help and support in making this release possible. Thank you for all the feedback
provided during the Developer Preview of .NET and Java support, without which the product would not
be what it is today.
Special thanks go out to a handful of amazing people who have provided instrumental support in
bringing .NET and Java support to this point:
Of course, we continue to be amazed and thrilled by the community contributions we received besides
language support. The passion demonstrated by the CDK community is heartwarming and largely
contributes to making maintaining the CDK an enjoyable, enriching experience!
Features
- lambda: node12.x, python3.8 and java11 runtimes (#5107) (e62f9fb)
- lambda: unlock the lambda environment variables restriction in China regions (#5122) (cc13009)
Bug Fixes
- init/chsarp: correct README for sample-app C# template (#5144) (b2031f6)
- init/sample-app: numerous fixes and additions to the sample-app init templates (#5119) (02c3b05), closes #5130 #5130
- init/java: add -e to mvn command so errors aren't hidden (#5129) (5427106), closes #5128
- init/csharp: .NET semantic fixes for init templates (#5154) (04a1b32)
Known Issues
The following known issues were identified that specifically affect .NET and Java support in the CDK,
and which will be promptly addressed in upcoming CDK releases (in no particular order). See the
GitHub issues for more information and workarounds where applicable.
- .NET and Java: [
aws/jsii#1011
] - abstract members are not marked as such on their .NET and Java representations - .NET: [
aws/jsii#1029
] - user-defined classes implementing CDK interfaces must extend Amazon.Jsii.Runtime.Deputy.DeputyBase
- .NET: [
aws/jsii#1042
] - Parameters typed object accept only primitive types, instances of CDK types, Dictionary<string,?>
- .NET: [
aws/jsii#1044
] - Unable to pass interface instance through in a Dictionary<string,object>
- Java: [
aws/jsii#1034
] - Implementing or overriding overloaded methods in Java does not work consistently - Java: [
aws/jsii#1035
] - Returning Lazy.anyValue
from an method whose return type is java.lang.Object
may result in Resolution Errors - Java: [
aws/jsii#1005
] - property getter implementations (e.g: from an interface) may be ignored