Source Class
Specifies bucket deployment source.
Usage:
Source.bucket(bucket, key)
Source.asset('/local/path/to/directory')
Source.asset('/local/path/to/a/file.zip')
Source.data('hello/world/file.txt', 'Hello, world!')
Source.jsonData('config.json', { baz: topic.topicArn })
Source.yamlData('config.yaml', { baz: topic.topicArn })
| Source | aws-s3-deployment/lib/source.ts |
|---|---|
| Type last changed |
Example
declare const destinationBucket: s3.Bucket;
const deployment = new s3deploy.BucketDeployment(this, 'DeployFiles', {
sources: [s3deploy.Source.asset(path.join(__dirname, 'source-files'))],
destinationBucket,
});
deployment.handlerRole.addToPolicy(
new iam.PolicyStatement({
actions: ['kms:Decrypt', 'kms:DescribeKey'],
effect: iam.Effect.ALLOW,
resources: ['<encryption key ARN>'],
}),
);
# destination_bucket: s3.Bucket
deployment = s3deploy.BucketDeployment(self, "DeployFiles",
sources=[s3deploy.Source.asset(path.join(__dirname, "source-files"))],
destination_bucket=destination_bucket
)
deployment.handler_role.add_to_policy(
iam.PolicyStatement(
actions=["kms:Decrypt", "kms:DescribeKey"],
effect=iam.Effect.ALLOW,
resources=["<encryption key ARN>"]
))
Bucket destinationBucket;
BucketDeployment deployment = BucketDeployment.Builder.create(this, "DeployFiles")
.sources(List.of(Source.asset(join(__dirname, "source-files"))))
.destinationBucket(destinationBucket)
.build();
deployment.handlerRole.addToPolicy(
PolicyStatement.Builder.create()
.actions(List.of("kms:Decrypt", "kms:DescribeKey"))
.effect(Effect.ALLOW)
.resources(List.of("<encryption key ARN>"))
.build());
// Install: dotnet add package Amazon.CDK.Lib
Bucket destinationBucket;
var deployment = new BucketDeployment(this, "DeployFiles", new BucketDeploymentProps {
Sources = new [] { Source.Asset(Join(__dirname, "source-files")) },
DestinationBucket = destinationBucket
});
deployment.HandlerRole.AddToPolicy(
new PolicyStatement(new PolicyStatementProps {
Actions = new [] { "kms:Decrypt", "kms:DescribeKey" },
Effect = Effect.ALLOW,
Resources = new [] { "<encryption key ARN>" }
}));
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var destinationBucket Bucket
deployment := s3deploy.NewBucketDeployment(this, jsii.String("DeployFiles"), &BucketDeploymentProps{
Sources: []ISource{
s3deploy.Source_Asset(path.join(__dirname, jsii.String("source-files"))),
},
DestinationBucket: DestinationBucket,
})
deployment.HandlerRole.AddToPolicy(
iam.NewPolicyStatement(&PolicyStatementProps{
Actions: []*string{
jsii.String("kms:Decrypt"),
jsii.String("kms:DescribeKey"),
},
Effect: iam.Effect_ALLOW,
Resources: []*string{
jsii.String("<encryption key ARN>"),
},
}))
Methods
asset(path: string, options?: AssetOptions): ISource@staticmethod def asset(path: str, options: typing.Optional[AssetOptions] = None) -> ISourcepublic static ISource asset(String path, AssetOptions options)public static ISource Asset(string path, AssetOptions? options = null)func Asset(Path *string, Options AssetOptions) ISource static #If the local asset is a .zip archive, make sure you trust the producer of the archive.
1 parameter
Parameters
pathpathpathpathPath— The path to a local .zip file or a directory.
bucket(bucket: IBucket, zipObjectKey: string): ISource@staticmethod def bucket(bucket: IBucket, zip_object_key: str) -> ISourcepublic static ISource bucket(IBucket bucket, String zipObjectKey)public static ISource Bucket(IBucket bucket, string zipObjectKey)func Bucket(Bucket IBucket, ZipObjectKey *string) ISource static #notes · 2 parameters · example
Make sure you trust the producer of the archive.
If the bucket parameter is an "out-of-app" reference "imported" via static methods such as s3.Bucket.fromBucketName, be cautious about the bucket's encryption key. In general, CDK does not query for additional properties of imported constructs at synthesis time. For example, for a bucket created from s3.Bucket.fromBucketName, CDK does not know its IBucket.encryptionKey property, and therefore will NOT give KMS permissions to the Lambda execution role of the BucketDeployment construct. If you want the kms:Decrypt and kms:DescribeKey permissions on the bucket's encryption key to be added automatically, reference the imported bucket via s3.Bucket.fromBucketAttributes and pass in the encryptionKey attribute explicitly.
Parameters
bucketbucketbucketbucketBucket— The S3 Bucket.zipObjectKeyzip_object_keyzipObjectKeyzipObjectKeyZipObjectKey— The S3 object key of the zip file with contents.
declare const destinationBucket: s3.Bucket;
const sourceBucket = s3.Bucket.fromBucketAttributes(this, 'SourceBucket', {
bucketArn: 'arn:aws:s3:::my-source-bucket-name',
encryptionKey: kms.Key.fromKeyArn(
this,
'SourceBucketEncryptionKey',
'arn:aws:kms:us-east-1:123456789012:key/<key-id>'
),
});
const deployment = new s3deploy.BucketDeployment(this, 'DeployFiles', {
sources: [s3deploy.Source.bucket(sourceBucket, 'source.zip')],
destinationBucket,
});
data(objectKey: string, data: string, markersConfig?: SourceDataOptions): ISource@staticmethod def data(object_key: str, data: str, markers_config: typing.Optional[SourceDataOptions] = None) -> ISourcepublic static ISource data(String objectKey, String data, SourceDataOptions markersConfig)public static ISource Data(string objectKey, string data, SourceDataOptions? markersConfig = null)func Data(ObjectKey *string, Data *string, MarkersConfig SourceDataOptions) ISource static #notes · 2 parameters
The content can include deploy-time values (such as snsTopic.topicArn) that will get resolved only during deployment.
The content is treated as an unstructured string, and deploy-time values will (mostly) be replaced with a plain string substitution. To store structured data, use either Source.jsonData() or Source.yamlData().
Default behavior
Due to CloudFormation behavior, scalars (strings, numbers and booleans) in Token values are all treated as strings at deploy time ("80", "true", etc).
- Scalars are plain-text substituted.
- Non-scalars (lists, objects) lead to a deployment error
JSON escaping
JSON escaping can be forced by specifying { jsonEscape: true }. Values are substituted as follows:
- Scalars come in as strings pre-encoded by CloudFormation; they are JSON-encoded, and then substituted without their surrounding quotes.
- Strings expect to go into a pre-quoted JSON string context (e.g.,
Source.data('{"arn": "' + snsTopic.topicArn + '"}')). - Non-strings expect to go into a non-quoted JSON context (e.g.,
Source.data('{"port": ' + cluster.port + '}')).
- Strings expect to go into a pre-quoted JSON string context (e.g.,
- Non-scalars are JSON-encoded and then fully substituted. They expect to go into a non-quoted JSON context in the input string (e.g.,
Source.data('{"ports": ' + Token.asString(cluster.allPorts) + '}')).
Parameters
objectKeyobject_keyobjectKeyobjectKeyObjectKey— The destination S3 object key (relative to the root of the S3 deployment).datadatadatadataData— The data to be stored in the object.
jsonData(objectKey: string, obj: any, jsonProcessingOptions?: JsonProcessingOptions): ISource@staticmethod def json_data(object_key: str, obj: Any, json_processing_options: typing.Optional[JsonProcessingOptions] = None) -> ISourcepublic static ISource jsonData(String objectKey, Object obj, JsonProcessingOptions jsonProcessingOptions)public static ISource JsonData(string objectKey, object obj, JsonProcessingOptions? jsonProcessingOptions = null)func JsonData(ObjectKey *string, Obj interface{}, JsonProcessingOptions JsonProcessingOptions) ISource static #notes · 3 parameters
The content can include deploy-time values (such as snsTopic.topicArn) that will get resolved only during deployment.
Quoting of deploy-time values will be done according to the ostensible type of the Token used to represent the deploy-time value.
- String tokens will be quoted into the JSON string skeleton, and deploy-time values will be encoded and substituted without their surrounding quotes. Will substitute correctly to working JSON if the deploy-time value is a string.
- Number and list tokens will be encoded and substituted without quotes. Will substitute correctly to working JSON if the deploy-time value is a number, boolean or list or map.
Note that due to CloudFormation behavior, scalars inside lists and maps will always be converted to strings.
Parameters
objectKeyobject_keyobjectKeyobjectKeyObjectKey— The destination S3 object key (relative to the root of the S3 deployment).objobjobjobjObj— A JSON object.jsonProcessingOptionsjson_processing_optionsjsonProcessingOptionsjsonProcessingOptionsJsonProcessingOptions— Options for how to process the JSON object.
yamlData(objectKey: string, obj: any): ISource@staticmethod def yaml_data(object_key: str, obj: Any) -> ISourcepublic static ISource yamlData(String objectKey, Object obj)public static ISource YamlData(string objectKey, object obj)func YamlData(ObjectKey *string, Obj interface{}) ISource static #notes · 2 parameters
The object can include deploy-time values (such as snsTopic.topicArn) that will get resolved only during deployment.
Quoting of deploy-time values will be done according to the ostensible type of the Token used to represent the deploy-time value.
- String tokens will be unquoted if they are simple strings (basically, identifiers), and quoted if they start to look complex.
- Number and list tokens will be encoded and substituted without quotes. Will substitute correctly to working JSON if the deploy-time value is a number, boolean or list or map.
Note that due to CloudFormation behavior, scalars inside lists and maps will always be converted to strings.
Parameters
objectKeyobject_keyobjectKeyobjectKeyObjectKey— The destination S3 object key (relative to the root of the S3 deployment).objobjobjobjObj— A JSON object.