id
stringlengths 14
16
| text
stringlengths 1
2.43k
| source
stringlengths 99
229
|
|---|---|---|
8da15bab7234-2
|
The AWS CDK's core module \(usually imported into code as `core` or `cdk`\) contains constructs used by the AWS CDK itself as well as base classes for constructs, apps, resources, and other AWS CDK objects\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
5c5ccabe52c9-0
|
The AWS CDK has first\-class support for TypeScript, JavaScript, Python, Java, and C\#\. \(Other JVM and \.NET CLR languages may also be used, at least in theory, but we are unable to offer support for them at this time\.\)
To facilitate supporting so many languages, the AWS CDK is developed in one language \(TypeScript\) and language bindings are generated for the other languages through the use of a tool called [JSII](https://github.com/aws/jsii)\.
We have taken pains to make AWS CDK app development in each language follow that language's usual conventions, so writing AWS CDK apps feels natural, not like writing TypeScript in Python \(for example\)\. Take a look:
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
22bfce7d0753-0
|
```
const bucket = new s3.Bucket(this, 'MyBucket', {
bucketName: 'my-bucket',
versioned: true,
websiteRedirect: {host: 'aws.amazon.com'}});
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
cb4e692fb908-0
|
```
const bucket = new s3.Bucket(this, 'MyBucket', {
bucketName: 'my-bucket',
versioned: true,
websiteRedirect: {host: 'aws.amazon.com'}});
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
75e23c811a4f-0
|
```
bucket = s3.Bucket(self, "MyBucket", bucket_name="my-bucket", versioned=true,
website_redirect=s3.WebsiteRedirect(host_name="aws.amazon.com"))
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
8eea7f313230-0
|
```
Bucket bucket = Bucket.Builder.create(self, "MyBucket")
.bucketName("my-bucket")
.versioned(true)
.websiteRedirect(new websiteRedirect.Builder()
.hostName("aws.amazon.com").build())
.build();
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
6f7a69da768e-0
|
```
var bucket = new Bucket(this, "MyBucket", new BucketProps {
BucketName = "my-bucket",
Versioned = true,
WebsiteRedirect = new WebsiteRedirect {
HostName = "aws.amazon.com"
}});
```
------
**Note**
These code snippets are intended for illustration only\. They are incomplete and won't run as they are\.
The AWS Construct Library is distributed using each language's standard package management tools, including NPM, PyPi, Maven, and NuGet\. There's even a version of the [AWS CDK API Reference](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html) for each language\.
To help you use the AWS CDK in your favorite language, this Guide includes topics that explain how to use the AWS CDK in all supported languages\.
+ [Working with the AWS CDK in TypeScript](work-with-cdk-typescript.md)
+ [Working with the AWS CDK in JavaScript](work-with-cdk-javascript.md)
+ [Working with the AWS CDK in Python](work-with-cdk-python.md)
+ [Working with the AWS CDK in Java](work-with-cdk-java.md)
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
6f7a69da768e-1
|
+ [Working with the AWS CDK in Java](work-with-cdk-java.md)
+ [Working with the AWS CDK in C\#](work-with-cdk-csharp.md)
Furthermore, since TypeScript was the first language supported by the AWS CDK, much AWS CDK example code is written in TypeScript\. For this reason, this Guide also includes a topic specifically to show how to adapt TypeScript AWS CDK code for use with the other supported languages\. See [Translating TypeScript AWS CDK code to other languages](multiple_languages.md)\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
5571981d21e3-0
|
With the concepts out of the way, here's what you need to have on your workstation before you install the AWS CDK and start developing\.
All CDK developers need to [install Node\.js](https://nodejs.org/en/download/) 10\.3\.0 or later, even those working in languages other than TypeScript or JavaScript\. The AWS CDK Toolkit \(cdk command\-line tool\) and the AWS Construct Library are developed in TypeScript and run on Node\.js\. The bindings for other supported languages use this back end and tool set\. We suggest the latest LTS version\.
**Important**
Node\.js versions 13\.0\.0 through 13\.6\.0 are not compatible with the AWS CDK\.
You must provide your credentials and an AWS Region to use AWS CDK, if you have not already done so\.
**Important**
We strongly recommend against using your AWS root account for day\-to\-day tasks\. Instead, create a user in IAM and use its credentials with the CDK\. Best practices are to change this account's access key regularly and to use a least\-privileges role \(specifying `--role-arn`\) when deploying\.
If you have the AWS CLI installed, the easiest way to satisfy this requirement is to install the AWS CLI and issue the following command:
```
aws configure
```
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
5571981d21e3-1
|
```
aws configure
```
Provide your AWS access key ID, secret access key, and default region when prompted\.
You may also manually create or edit the `~/.aws/config` and `~/.aws/credentials` \(Mac OS X or Linux\) or `%USERPROFILE%\.aws\config` and `%USERPROFILE%\.aws\credentials` \(Windows\) files to contain credentials and a default region, in the following format\.
+ In `~/.aws/config` or `%USERPROFILE%\.aws\config`
```
[default]
region=us-west-2
```
+ In `~/.aws/credentials` or `%USERPROFILE%\.aws\credentials`
```
[default]
aws_access_key_id=AKIAI44QH8DHBEXAMPLE
aws_secret_access_key=je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
```
Finally, you can set the environment variables `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and `AWS_DEFAULT_REGION` to appropriate values\.
Other prerequisites depend on your development language and are as follows\.
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
8fe48d448f60-0
|
+ TypeScript 2\.7 or later \(`npm -g install typescript`\)
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
c1c6b394142e-0
|
No additional requirements
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
164e7d2e1783-0
|
+ Python 3\.6 or later including `pip` and `virtualenv`
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
657906ef3714-0
|
+ Java Development Kit \(JDK\) 8 \(a\.k\.a\. 1\.8\) or later
+ Apache Maven 3\.5 or later
Java IDE recommended \(we use Eclipse in some examples in this Developer Guide\)\. IDE must be able to import Maven projects\. Check to make sure your project is set to use Java 1\.8\. Set the JAVA\_HOME environment variable to the path where you have installed the JDK\.
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
fdd3fcf40cd8-0
|
A \.NET Standard 2\.1\-compatible implementation is required, such as\.
+ \.NET Core 3\.1 or later
+ \.NET Framework 4\.6\.1 or later
+ Mono 5\.4 or later
Visual Studio 2019 \(any edition\) recommended\.
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
ad0d7e123a0d-0
|
Install the AWS CDK Toolkit globally using the following Node Package Manager command\.
```
npm install -g aws-cdk
```
Run the following command to verify correct installation and print the version number of the AWS CDK\.
```
cdk --version
```
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
859f95cf6e9f-0
|
We've already been using the AWS CDK Toolkit, also known as the Command Line Interface \(CLI\)\. It's the main tool you use to interact with your AWS CDK app\. It executes the AWS CDK app you wrote and compiled, interrogates the application model you defined, and produces and deploys the AWS CDK templates it generates\. It also has deployment, diff, deletion, and troubleshooting capabilities\. For more information, see cdk \-\-help or [AWS CDK Toolkit \(`cdk` command\)](cli.md)\.
The [AWS Toolkit for Visual Studio Code](https://aws.amazon.com/visualstudiocode/) is an open\-source plug\-in for Visual Studio Code that makes it easier to create, debug, and deploy applications on AWS\. The toolkit provides an integrated experience for developing AWS CDK applications, including the AWS CDK Explorer feature to list your AWS CDK projects and browse the various components of the CDK application\. [Install the plug\-in](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/setup-toolkit.html) and learn more about [using the AWS CDK Explorer](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/cdk-explorer.html)\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
2c5a8c794333-0
|
Where do you go now that you've dipped your toes in the AWS CDK?
+ Come on in; the water's fine\! Build [your first AWS CDK app](hello_world.md)\.
+ Try the [CDK Workshop](https://cdkworkshop.com/) for a more in\-depth tour involving a more complex project\.
+ See the [API reference](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html) to begin exploring the CDK constructs available for your favorite AWS services\.
+ Dig deeper into concepts like [Environments](environments.md), [Assets](assets.md), [Bootstrapping](bootstrapping.md), [Permissions](permissions.md), [Runtime context](context.md), [Parameters](parameters.md), and [Escape hatches](cfn_layer.md)\.
+ Explore [Examples](https://github.com/aws-samples/aws-cdk-examples) of using the AWS CDK\.
The AWS CDK is an open\-source project\. Want to [contribute](https://github.com/aws/aws-cdk)?
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/getting_started.md
|
190c0ad2cd8f-0
|
The Amazon Web Services \(AWS\) global infrastructure is built around AWS Regions and Availability Zones\.
AWS Regions provide multiple physically separated and isolated Availability Zones, which are connected with low\-latency, high\-throughput, and highly redundant networking\.
With Availability Zones, you can design and operate applications and databases that automatically fail over between Availability Zones without interruption\. Availability Zones are more highly available, fault tolerant, and scalable than traditional single or multiple data center infrastructures\.
For more information about AWS Regions and Availability Zones, see [AWS Global Infrastructure](https://aws.amazon.com/about-aws/global-infrastructure/)\.
The AWS CDK follows the [shared responsibility model](https://aws.amazon.com/compliance/shared-responsibility-model/) through the specific Amazon Web Services \(AWS\) services it supports\. For AWS service security information, see the [AWS service security documentation page](https://docs.aws.amazon.com/security/?id=docs_gateway#aws-security) and [AWS services that are in scope of AWS compliance efforts by compliance program](https://aws.amazon.com/compliance/services-in-scope/)\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/disaster-recovery-resiliency.md
|
68150d48fd3e-0
|
Constructs are the basic building blocks of AWS CDK apps\. A construct represents a "cloud component" and encapsulates everything AWS CloudFormation needs to create the component\.
A construct can represent a single resource, such as an Amazon Simple Storage Service \(Amazon S3\) bucket, or it can represent a higher\-level component consisting of multiple AWS CDK resources\. Examples of such components include a worker queue with its associated compute capacity, a cron job with monitoring resources and a dashboard, or even an entire app spanning multiple AWS accounts and regions\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
c0ef023dc7d8-0
|
The AWS CDK includes the [AWS Construct Library](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html), which contains constructs representing AWS resources\.
This library includes constructs that represent all the resources available on AWS\. For example, the `[s3\.Bucket](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html)` class represents an Amazon S3 bucket, and the `[dynamodb\.Table](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.Table.html)` class represents an Amazon DynamoDB table\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
c0ef023dc7d8-1
|
There are different levels of constructs in this library, beginning with low\-level constructs, which we call *CFN Resources* \(or L1, short for "level 1"\)\. These constructs directly represent all of the AWS resources that are available in AWS CloudFormation\. CFN Resources are periodically generated from the [AWS CloudFormation Resource Specification](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-resource-specification.html)\. They are named **Cfn***Xyz*, where *Xyz* is name of the resource\. For example, [s3\.CfnBucket](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.CfnBucket.html) represents the [AWS::S3::Bucket](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket.html) CFN Resource\. When you use CFN resources, you must explicitly configure all resource properties, which requires a complete understanding of the details of the underlying AWS CloudFormation resource model\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
c0ef023dc7d8-2
|
The next level of constructs, L2, also represent AWS resources, but with a higher\-level, intent\-based API\. They provide similar functionality, but provide the defaults, boilerplate, and glue logic you'd be writing yourself with a CFN Resource construct\. AWS constructs offer convenient defaults and reduce the need to know all the details about the AWS resources they represent, while providing convenience methods that make it simpler to work with the resource\. For example, the [s3\.Bucket](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html) class represents an Amazon S3 bucket with additional properties and methods, such as [bucket\.addLifeCycleRule\(\)](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html#add-wbr-lifecycle-wbr-rulerule), which adds a lifecycle rule to the bucket\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
c0ef023dc7d8-3
|
Finally, the AWS Construct Library includes even higher\-level constructs, which we call *patterns*\. These constructs are designed to help you complete common tasks in AWS, often involving multiple kinds of resources\. For example, the [aws\-ecs\-patterns\.ApplicationLoadBalancedFargateService](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ecs-patterns.ApplicationLoadBalancedFargateService.html) construct represents an architecture that includes an AWS Fargate container cluster employing an Application Load Balancer \(ALB\)\. The [aws\-apigateway\.LambdaRestApi](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-apigateway.LambdaRestApi.html) construct represents an Amazon API Gateway API that's backed by an AWS Lambda function\.
For more information about how to navigate the library and discover constructs that can help you build your apps, see the [API Reference](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html)\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
210ab98b0af6-0
|
The key pattern for defining higher\-level abstractions through constructs is called *composition*\. A high\-level construct can be composed from any number of lower\-level constructs, and in turn, those could be composed from even lower\-level constructs\. To enable this pattern, constructs are always defined within the scope of another construct\. This scoping pattern results in a hierarchy of constructs known as a *construct tree*\. In the AWS CDK, the root of the tree represents your entire **[AWS CDK app](apps.md)**\. Within the app, you typically define one or more **[stacks](stacks.md)**, which are the unit of deployment, analogous to AWS CloudFormation stacks\. Within stacks, you define resources, or other constructs that eventually contain resources\.
Composition of constructs means that you can define reusable components and share them like any other code\. For example, a central team can define a construct that implements the company's best practice for a DynamoDB table with backup, global replication, auto\-scaling, and monitoring, and share it with teams across a company or publicly\. Teams can now use this construct as they would any other library package in their favorite programming language to define their tables and comply with their team's best practices\. When the library is updated, developers can pick up the updates and enjoy any bug fixes and improvements through the workflows they already have for their other types of code\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
f2ae691f2aa2-0
|
Constructs are implemented in classes that extend the [https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Construct.html](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Construct.html) base class\. You define a construct by instantiating the class\. All constructs take three parameters when they are initialized:
+ **Scope** – The construct within which this construct is defined\. You should almost always pass `this` for the scope, because it represents the current scope in which you are defining the construct\.
+ **id** – An [identifier](identifiers.md) that must be unique within this scope\. The identifier serves as a namespace for everything that's encapsulated within the scope's subtree and is used to allocate unique identities such as [resource names](resources.md#resources_physical_names) and AWS CloudFormation logical IDs\.
+ **Props** – A set of properties or keyword arguments, depending upon the supported language, that define the construct's initial configuration\. In most cases, constructs provide sensible defaults, and if all props elements are optional, you can leave out the **props** parameter completely\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
f2ae691f2aa2-1
|
Identifiers need only be unique within a scope\. This lets you instantiate and reuse constructs without concern for the constructs and identifiers they might contain, and enables composing constructs into higher level abstractions\. In addition, scopes make it possible to refer to groups of constructs all at once, for example for [tagging](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Tag.html) or for specifying where the constructs will be deployed\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
4d80a6085ebb-0
|
We call your CDK application an *app*, which is represented by the AWS CDK class [App](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.App.html)\. The following example defines an app with a single stack that contains a single Amazon S3 bucket with versioning enabled:
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
f652baddc6ec-0
|
```
import { App, Stack, StackProps } from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';
class HelloCdkStack extends Stack {
constructor(scope: App, id: string, props?: StackProps) {
super(scope, id, props);
new s3.Bucket(this, 'MyFirstBucket', {
versioned: true
});
}
}
const app = new App();
new HelloCdkStack(app, "HelloCdkStack");
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
8c0035dd75d0-0
|
```
const { App , Stack } = require('@aws-cdk/core');
const s3 = require('@aws-cdk/aws-s3');
class HelloCdkStack extends Stack {
constructor(scope, id, props) {
super(scope, id, props);
new s3.Bucket(this, 'MyFirstBucket', {
versioned: true
});
}
}
const app = new App();
new HelloCdkStack(app, "HelloCdkStack");
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
5b5343593c7e-0
|
```
from aws_cdk.core import App, Stack
from aws_cdk import aws_s3 as s3
class HelloCdkStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
s3.Bucket(self, "MyFirstBucket", versioned=True)
app = core.App()
HelloCdkStack(app, "HelloCdkStack")
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
3593750ff5f0-0
|
```
import software.amazon.awscdk.core.*;
import software.amazon.awscdk.services.s3.*;
public class HelloCdkStack extends Stack {
public HelloCdkStack(final Construct scope, final String id) {
this(scope, id, null);
}
public HelloCdkStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
Bucket.Builder.create(this, "MyFirstBucket")
.versioned(true).build();
}
}
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
50b6c8ecf300-0
|
```
using Amazon.CDK;
using Amazon.CDK.AWS.S3;
namespace HelloCdkApp
{
internal static class Program
{
public static void Main(string[] args)
{
var app = new App();
new HelloCdkStack(app, "HelloCdkStack");
app.Synth();
}
}
public class HelloCdkStack : Stack
{
public HelloCdkStack(Construct scope, string id, IStackProps props=null) : base(scope, id, props)
{
new Bucket(this, "MyFirstBucket", new BucketProps { Versioned = true });
}
}
}
```
------
As you can see, you need a scope within which to define your bucket\. Since resources eventually need to be deployed as part of a AWS CloudFormation stack into an *AWS [environment](environments.md)*, which covers a specific AWS account and AWS region\. AWS constructs, such as `s3.Bucket`, must be defined within the scope of a [Stack](https://docs.aws.amazon.com/cdk/api/latest/typescript/api/core/stack.html)\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
50b6c8ecf300-1
|
Stacks in AWS CDK apps extend the **Stack** base class, as shown in the previous example\. This is a common pattern when creating a stack within your AWS CDK app: extend the **Stack** class, define a constructor that accepts **scope**, **id**, and **props**, and invoke the base class constructor via `super` with the received **scope**, **id**, and **props**, as shown in the following example\.
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
1162a2a14e57-0
|
```
class HelloCdkStack extends Stack {
constructor(scope: App, id: string, props?: StackProps) {
super(scope, id, props);
//...
}
}
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
eea3f1fba425-0
|
```
class HelloCdkStack extends Stack {
constructor(scope, id, props) {
super(scope, id, props);
//...
}
}
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
203b4d27e41d-0
|
```
class HelloCdkStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
c22196c1085c-0
|
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
44853a7b975c-0
|
```
public class HelloCdkStack extends Stack {
public HelloCdkStack(final Construct scope, final String id) {
this(scope, id, null);
}
public HelloCdkStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
// ...
}
}
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
6ad12d3b2d6d-0
|
```
public class HelloCdkStack : Stack
{
public HelloCdkStack(Construct scope, string id, IStackProps props=null) : base(scope, id, props)
{
//...
}
}
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
69d8a987f4bf-0
|
Once you have defined a stack, you can populate it with resources by instantiating constructs\. First, we'll do it with an L1 construct\.
L1 constructs are exactly the resources defined by AWS CloudFormation—no more, no less\. You must provide the resource's required configuration yourself\. Here, for example, is how to create an Amazon S3 bucket using the `CfnBucket` class\. \(You'll see a similar definition using the `Bucket` class in the next section\.\)
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
54b8f9bde9ce-0
|
```
const bucket = new s3.CfnBucket(this, "MyBucket", {
bucketName: "MyBucket"
});
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
23980b3d6c05-0
|
```
const bucket = new s3.CfnBucket(this, "MyBucket", {
bucketName: "MyBucket"
});
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
710b65722b2d-0
|
```
bucket = s3.CfnBucket(self, "MyBucket", bucket_name="MyBucket")
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
ea6b7841b2cd-0
|
```
CfnBucket bucket = new CfnBucket.Builder().bucketName("MyBucket").build();
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
4e87451910d7-0
|
```
var bucket = new CfnBucket(this, "MyBucket", new CfnBucketProps
{
BucketName= "MyBucket"
});
```
------
In Python, Java, and C\#, L1 construct properties that aren't simple Booleans, strings, numbers, or containers are represented by types defined as inner classes of the L1 construct\. For example, the optional property `corsConfiguration` of a `CfnBucket` requires a wrapper of type `Cfn.CorsConfigurationProperty`\. Here we are defining `corsConfiguration` on a `CfnBucket` instance\.
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
0c86d8c444d4-0
|
```
const bucket = new s3.CfnBucket(this, "MyBucket", {
bucketName: "MyBucket",
corsConfiguration: {
corsRules: [{
allowedOrigins: ["*"],
allowedMethods: ["*"]
}]
}
});
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
f34ef61147a0-0
|
```
const bucket = new s3.CfnBucket(this, "MyBucket", {
bucketName: "MyBucket",
corsConfiguration: {
corsRules: [{
allowedOrigins: ["*"],
allowedMethods: ["*"]
}]
}
});
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
9ba44d1bbd2d-0
|
```
bucket = CfnBucket(self, "MyBucket", bucket_name="MyBucket",
cors_configuration=CfnBucket.CorsConfigurationProperty(
cors_rules=[CfnBucket.CorsRuleProperty(
allowed_origins=["*"],
allowed_methods=["GET"]
)]
)
)
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
9e2a2691807a-0
|
```
CfnBucket bucket = CfnBucket.Builder.create(this, "MyBucket")
.bucketName("MyBucket")
.corsConfiguration(new CfnBucket.CorsConfigurationProperty.Builder()
.corsRules(Arrays.asList(new CfnBucket.CorsRuleProperty.Builder()
.allowedOrigins(Arrays.asList("*"))
.allowedMethods(Arrays.asList("GET"))
.build()))
.build())
.build();
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
a369b0287a1b-0
|
```
var bucket = new CfnBucket(this, "MyBucket", new CfnBucketProps
{
BucketName = "MyBucket",
CorsConfiguration = new CfnBucket.CorsConfigurationProperty
{
CorsRules = new object[] {
new CfnBucket.CorsRuleProperty
{
AllowedOrigins = new string[] { "*" },
AllowedMethods = new string[] { "GET" },
}
}
}
});
```
------
**Important**
You can't use L2 property types with L1 constructs, or vice versa\. When working with L1 constructs, always use the types defined inside the L1 construct you're using\. Do not use types from other L1 constructs \(some may have the same name, but they are not the same type\)\.
Some of our language\-specific API references currently have errors in the paths to L1 property types, or don't document these classes at all\. We hope to fix this soon\. In the meantime, just remember that such types are always inner classes of the L1 construct they are used with\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
9580657af689-0
|
The following example defines an Amazon S3 bucket by creating an instance of the [https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html) class, an L2 construct\.
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
1aa399426566-0
|
```
import * as s3 from '@aws-cdk/aws-s3';
// "this" is HelloCdkStack
new s3.Bucket(this, 'MyFirstBucket', {
versioned: true
});
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
56f0fd36b541-0
|
```
const s3 = require('@aws-cdk/aws-s3');
// "this" is HelloCdkStack
new s3.Bucket(this, 'MyFirstBucket', {
versioned: true
});
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
ffd92aff4a09-0
|
```
from aws_cdk import aws_s3 as s3
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
38b60eb13f86-0
|
s3.Bucket(self, "MyFirstBucket", versioned=True)
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
5b8882d3c650-0
|
```
import software.amazon.awscdk.services.s3.*;
public class HelloCdkStack extends Stack {
public HelloCdkStack(final Construct scope, final String id) {
this(scope, id, null);
}
public HelloCdkStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
Bucket.Builder.create(this, "MyFirstBucket")
.versioned(true).build();
}
}
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
2c13e1db5fa3-0
|
```
using Amazon.CDK.AWS.S3;
// "this" is HelloCdkStack
new Bucket(this, "MyFirstBucket", new BucketProps
{
Versioned = true
});
```
------
The [AWS Construct Library](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html) includes constructs that represent many AWS resources\.
**Note**
`MyFirstBucket` is not the name of the bucket that AWS CloudFormation creates\. It is a logical identifier given to the new construct\. See [Physical Names](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Resource.html#physicalname-span-class-api-icon-api-icon-experimental-title-this-api-element-is-experimental-it-may-change-without-notice-span) for details\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
0056dd7bd0fa-0
|
Most constructs accept `props` as their third argument \(or in Python, keyword arguments\), a name/value collection that defines the construct's configuration\. The following example defines a bucket with AWS Key Management Service \(AWS KMS\) encryption and static website hosting enabled\. Since it does not explicitly specify an encryption key, the `Bucket` construct defines a new `kms.Key` and associates it with the bucket\.
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
1c30ab2f6aac-0
|
```
new s3.Bucket(this, 'MyEncryptedBucket', {
encryption: s3.BucketEncryption.KMS,
websiteIndexDocument: 'index.html'
});
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
9232480b66f9-0
|
```
new s3.Bucket(this, 'MyEncryptedBucket', {
encryption: s3.BucketEncryption.KMS,
websiteIndexDocument: 'index.html'
});
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
d9a73af8ead9-0
|
```
s3.Bucket(self, "MyEncryptedBucket", encryption=s3.BucketEncryption.KMS,
website_index_document="index.html")
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
15f9a19df335-0
|
```
Bucket.Builder.create(this, "MyEncryptedBucket")
.encryption(BucketEncryption.KMS_MANAGED)
.websiteIndexDocument("index.html").build();
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
a13beaf136f4-0
|
```
new Bucket(this, "MyEncryptedBucket", new BucketProps
{
Encryption = BucketEncryption.KMS_MANAGED,
WebsiteIndexDocument = "index.html"
});
```
------
AWS constructs are designed around the concept of "sensible defaults\." Most constructs have a minimal required configuration, enabling you to quickly get started while also providing full control over the configuration when you need it\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
59f1a06c0ff7-0
|
Constructs are classes that extend the base [Construct](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Construct.html) class\. After you instantiate a construct, the construct object exposes a set of methods and properties that enable you to interact with the construct and pass it around as a reference to other parts of the system\. The AWS CDK framework doesn't put any restrictions on the APIs of constructs; authors can define any API they wish\. However, the AWS constructs that are included with the AWS Construct Library, such as `s3.Bucket`, follow guidelines and common patterns in order to provide a consistent experience across all AWS resources\.
For example, almost all AWS constructs have a set of [grant](permissions.md#permissions_grants) methods that you can use to grant AWS Identity and Access Management \(IAM\) permissions on that construct to a principal\. The following example grants the IAM group `data-science` permission to read from the Amazon S3 bucket `raw-data`\.
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
88eddbfd816f-0
|
```
const rawData = new s3.Bucket(this, 'raw-data');
const dataScience = new iam.Group(this, 'data-science');
rawData.grantRead(dataScience);
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
e8c154e0e4c7-0
|
```
const rawData = new s3.Bucket(this, 'raw-data');
const dataScience = new iam.Group(this, 'data-science');
rawData.grantRead(dataScience);
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
c02c0414c601-0
|
```
raw_data = s3.Bucket(self, 'raw-data')
data_science = iam.Group(self, 'data-science')
raw_data.grant_read(data_science)
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
ff6e5635c088-0
|
```
Bucket rawData = new Bucket(this, "raw-data");
Group dataScience = new Group(this, "data-science");
rawData.grantRead(dataScience);
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
c3393edafb1c-0
|
```
var rawData = new Bucket(this, "raw-data");
var dataScience = new Group(this, "data-science");
rawData.GrantRead(dataScience);
```
------
Another common pattern is for AWS constructs to set one of the resource's attributes, such as its Amazon Resource Name \(ARN\), name, or URL from data supplied elsewhere\. For example, the following code defines an AWS Lambda function and associates it with an Amazon Simple Queue Service \(Amazon SQS\) queue through the queue's URL in an environment variable\.
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
8e06d77a0ac0-0
|
```
const jobsQueue = new sqs.Queue(this, 'jobs');
const createJobLambda = new lambda.Function(this, 'create-job', {
runtime: lambda.Runtime.NODEJS_10_X,
handler: 'index.handler',
code: lambda.Code.fromAsset('./create-job-lambda-code'),
environment: {
QUEUE_URL: jobsQueue.queueUrl
}
});
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
0908568fc81f-0
|
```
const jobsQueue = new sqs.Queue(this, 'jobs');
const createJobLambda = new lambda.Function(this, 'create-job', {
runtime: lambda.Runtime.NODEJS_10_X,
handler: 'index.handler',
code: lambda.Code.fromAsset('./create-job-lambda-code'),
environment: {
QUEUE_URL: jobsQueue.queueUrl
}
});
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
4fb8bcf263ed-0
|
```
jobs_queue = sqs.Queue(self, "jobs")
create_job_lambda = lambda_.Function(self, "create-job",
runtime=lambda_.Runtime.NODEJS_10_X,
handler="index.handler",
code=lambda_.Code.from_asset("./create-job-lambda-code"),
environment=dict(
QUEUE_URL=jobs_queue.queue_url
)
)
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
7f8d220f28d9-0
|
```
final Queue jobsQueue = new Queue(this, "jobs");
Function createJobLambda = Function.Builder.create(this, "create-job")
.handler("index.handler")
.code(Code.fromAsset("./create-job-lambda-code"))
.environment(new HashMap<String, String>() {{
put("QUEUE_URL", jobsQueue.getQueueUrl());
}}).build();
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
251e4425dfb8-0
|
```
var jobsQueue = new Queue(this, "jobs");
var createJobLambda = new Function(this, "create-job", new FunctionProps
{
Runtime = Runtime.NODEJS_10_X,
Handler = "index.handler",
Code = Code.FromAsset(@".\create-job-lambda-code"),
Environment = new Dictionary<string, string>
{
["QUEUE_URL"] = jobsQueue.QueueUrl
}
});
```
------
For information about the most common API patterns in the AWS Construct Library, see [Resources](resources.md)\.
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
05f29df88e54-0
|
In addition to using existing constructs like `s3.Bucket`, you can also author your own constructs, and then anyone can use them in their apps\. All constructs are equal in the AWS CDK\. An AWS CDK construct such as `s3.Bucket` or `sns.Topic` behaves the same as a construct imported from a third\-party library that someone published on npm or Maven or PyPI—or to your company's internal package repository\.
To declare a new construct, create a class that extends the [Construct](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Construct.html) base class, then follow the pattern for initializer arguments\.
For example, you could declare a construct that represents an Amazon S3 bucket which sends an Amazon Simple Notification Service \(Amazon SNS\) notification every time someone uploads a file into it:
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
ab9478357591-0
|
```
export interface NotifyingBucketProps {
prefix?: string;
}
export class NotifyingBucket extends Construct {
constructor(scope: Construct, id: string, props: NotifyingBucketProps = {}) {
super(scope, id);
const bucket = new s3.Bucket(this, 'bucket');
const topic = new sns.Topic(this, 'topic');
bucket.addObjectCreatedNotification(new s3notify.SnsDestination(topic),
{ prefix: props.prefix });
}
}
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
8c99ae1f30a4-0
|
```
class NotifyingBucket extends Construct {
constructor(scope, id, props = {}) {
super(scope, id);
const bucket = new s3.Bucket(this, 'bucket');
const topic = new sns.Topic(this, 'topic');
bucket.addObjectCreatedNotification(new s3notify.SnsDestination(topic),
{ prefix: props.prefix });
}
}
module.exports = { NotifyingBucket }
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
586aa157e717-0
|
```
class NotifyingBucket(core.Construct):
def __init__(self, scope: core.Construct, id: str, *, prefix=None):
super().__init__(scope, id)
bucket = s3.Bucket(self, "bucket")
topic = sns.Topic(self, "topic")
bucket.add_object_created_notification(s3notify.SnsDestination(topic),
s3.NotificationKeyFilter(prefix=prefix))
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
788806af97b2-0
|
```
public class NotifyingBucket extends Construct {
public NotifyingBucket(final Construct scope, final String id) {
this(scope, id, null, null);
}
public NotifyingBucket(final Construct scope, final String id, final BucketProps props) {
this(scope, id, props, null);
}
public NotifyingBucket(final Construct scope, final String id, final String prefix) {
this(scope, id, null, prefix);
}
public NotifyingBucket(final Construct scope, final String id, final BucketProps props, final String prefix) {
super(scope, id);
Bucket bucket = new Bucket(this, "bucket");
Topic topic = new Topic(this, "topic");
if (prefix != null)
bucket.addObjectCreatedNotification(new SnsDestination(topic),
NotificationKeyFilter.builder().prefix(prefix).build());
}
}
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
d02c88fddd97-0
|
```
public class NotifyingBucketProps : BucketProps
{
public string Prefix { get; set; }
}
public class NotifyingBucket : Construct
{
public NotifyingBucket(Construct scope, string id, NotifyingBucketProps props = null) : base(scope, id)
{
var bucket = new Bucket(this, "bucket");
var topic = new Topic(this, "topic");
bucket.AddObjectCreatedNotification(new SnsDestination(topic), new NotificationKeyFilter
{
Prefix = props?.Prefix
});
}
}
```
------
The `NotifyingBucket` constructor has a signature compatible with the base `Construct` class: `scope`, `id`, and `props`\. The last argument, `props`, is optional \(gets the default value `{}`\) because all props are optional\. This means that you could define an instance of this construct in your app without `props`, for example:
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
4346d0512782-0
|
```
new NotifyingBucket(this, 'MyNotifyingBucket');
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
1523b8b5ebfe-0
|
```
new NotifyingBucket(this, 'MyNotifyingBucket');
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
ef4f7520840a-0
|
```
NotifyingBucket(self, "MyNotifyingBucket")
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
9bdcd976f332-0
|
```
new NotifyingBucket(this, "MyNotifyingBucket");
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
795a7ebf1dc6-0
|
```
new NotifyingBucket(this, "MyNotifyingBucket");
```
------
Or you could use `props` \(in Java, an additional parameter\) to specify the path prefix to filter on, for example:
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
6cd29d3e159f-0
|
```
new NotifyingBucket(this, 'MyNotifyingBucket', { prefix: 'images/' });
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
795cfb3fb982-0
|
```
new NotifyingBucket(this, 'MyNotifyingBucket', { prefix: 'images/' });
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
1ea28fff3533-0
|
```
NotifyingBucket(self, "MyNotifyingBucket", prefix="images/")
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
6917dfc1c307-0
|
```
new NotifyingBucket(this, "MyNotifyingBucket", "/images");
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
ab10213e4d68-0
|
```
new NotifyingBucket(this, "MyNotifyingBucket", new NotifyingBucketProps
{
Prefix = "/images"
});
```
------
Typically, you would also want to expose some properties or methods on your constructs\. For example, it's not very useful to have a topic hidden behind your construct, because it wouldn't be possible for users of your construct to subscribe to it\. Adding a `topic` property allows consumers to access the inner topic, as shown in the following example:
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
e03d11bcfa5e-0
|
```
export class NotifyingBucket extends Construct {
public readonly topic: sns.Topic;
constructor(scope: Construct, id: string, props: NotifyingBucketProps) {
super(scope, id, props);
const bucket = new s3.Bucket(this, 'bucket');
this.topic = new sns.Topic(this, 'topic');
bucket.addObjectCreatedNotification(new s3notify.SnsDestination(this.topic), { prefix: props.prefix });
}
}
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
a8ea765a9000-0
|
```
class NotifyingBucket extends Construct {
constructor(scope, id, props) {
super(scope, id, props);
const bucket = new s3.Bucket(this, 'bucket');
this.topic = new sns.Topic(this, 'topic');
bucket.addObjectCreatedNotification(new s3notify.SnsDestination(this.topic), { prefix: props.prefix });
}
}
module.exports = { NotifyingBucket }
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
e1ebfe0e6dd8-0
|
```
class NotifyingBucket(core.Construct):
def __init__(self, scope: core.Construct, id: str, *, prefix=None, **kwargs):
super().__init__(scope, id, **kwargs)
bucket = s3.Bucket(self, "bucket")
self.topic = sns.Topic(self, "topic")
bucket.add_object_created_notification(s3notify.SnsDestination(self.topic),
s3.NotificationKeyFilter(prefix=prefix))
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
9fa6a9de572e-0
|
```
public class NotifyingBucket extends Bucket {
public Topic topic = null;
public NotifyingBucket(final Construct scope, final String id) {
this(scope, id, null, null);
}
public NotifyingBucket(final Construct scope, final String id, final BucketProps props) {
this(scope, id, props, null);
}
public NotifyingBucket(final Construct scope, final String id, final String prefix) {
this(scope, id, null, prefix);
}
public NotifyingBucket(final Construct scope, final String id, final BucketProps props, final String prefix) {
super(scope, id, props);
Bucket bucket = new Bucket(this, "bucket");
topic = new Topic(this, "topic");
if (prefix != null)
bucket.addObjectCreatedNotification(new SnsDestination(topic),
NotificationKeyFilter.builder().prefix(prefix).build());
}
}
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
d60ab0a47d29-0
|
```
public class NotifyingBucket : Construct
{
public readonly Topic topic;
public NotifyingBucket(Construct scope, string id, NotifyingBucketProps props = null) : base(scope, id, props)
{
var bucket = new Bucket(this, "bucket");
topic = new Topic(this, "topic");
bucket.AddObjectCreatedNotification(new SnsDestination(topic), new NotificationKeyFilter
{
Prefix = props?.Prefix
});
}
}
```
------
Now, consumers can subscribe to the topic, for example:
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
f59b26ea6c13-0
|
```
const queue = new sqs.Queue(this, 'NewImagesQueue');
const images = new NotifyingBucket(this, '/images');
images.topic.addSubscription(new sns_sub.SqsSubscription(queue));
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
bb8f7998f8b3-0
|
```
const queue = new sqs.Queue(this, 'NewImagesQueue');
const images = new NotifyingBucket(this, '/images');
images.topic.addSubscription(new sns_sub.SqsSubscription(queue));
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
8dc2ebd83dce-0
|
```
queue = sqs.Queue(self, "NewImagesQueue")
images = NotifyingBucket(self, prefix="Images")
images.topic.add_subscription(sns_sub.SqsSubscription(queue))
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
7f1a3f886812-0
|
```
NotifyingBucket images = new NotifyingBucket(this, "MyNotifyingBucket", "/images");
images.topic.addSubscription(new SqsSubscription(queue));
```
------
|
https://github.com/siagholami/aws-documentation/tree/main/documents/aws-cdk-guide/doc_source/constructs.md
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.