AWS CDK ์‚ฌ์šฉ๋ฒ•
Devops

AWS CDK ์‚ฌ์šฉ๋ฒ•

1. cdk init

- ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ

- cdk ์ดˆ๊ธฐํ™”(์‚ฌ์šฉํ•  ์–ธ์–ด ์˜ต์…˜์œผ๋กœ ์ง€์ •)

> cdk init app --language=python
Applying project template app for python

# Welcome to your CDK Python project!

This is a blank project for Python development with CDK.

The `cdk.json` file tells the CDK Toolkit how to execute your app.

This project is set up like a standard Python project.  The initialization
process also creates a virtualenv within this project, stored under the `.venv`
directory.  To create the virtualenv it assumes that there is a `python3`
(or `python` for Windows) executable in your path with access to the `venv`
package. If for any reason the automatic creation of the virtualenv fails,
you can create the virtualenv manually.

To manually create a virtualenv on MacOS and Linux:

```
$ python3 -m venv .venv
```

After the init process completes and the virtualenv is created, you can use the following
step to activate your virtualenv.

```
$ source .venv/bin/activate
```

If you are a Windows platform, you would activate the virtualenv like this:

```
% .venv\Scripts\activate.bat
```

Once the virtualenv is activated, you can install the required dependencies.

```
$ pip install -r requirements.txt
```

At this point you can now synthesize the CloudFormation template for this code.

```
$ cdk synth
```

To add additional dependencies, for example other CDK libraries, just add
them to your `setup.py` file and rerun the `pip install -r requirements.txt`
command.

## Useful commands

 * `cdk ls`          list all stacks in the app
 * `cdk synth`       emits the synthesized CloudFormation template
 * `cdk deploy`      deploy this stack to your default AWS account/region
 * `cdk diff`        compare deployed stack with current state
 * `cdk docs`        open CDK documentation

Enjoy!

Initializing a new git repository...
Please run 'python3 -m venv .venv'!
Executing Creating virtualenv...
โœ… All done!

- ํŒŒ์ด์ฌ venv ํ™œ์„ฑํ™”

- cdk๋ฅผ ์œ„ํ•œ ๋ณ„๋„ ํŒŒ์ด์ฌ ํ™˜๊ฒฝ์ด ์„ธํŒ… ๋จ

- ํŒŒ์ผ ๋ฆฌ์ŠคํŠธ

2. cdk ls

- stack ๋ฆฌ์ŠคํŠธ ํ™•์ธ ๊ฐ€๋Šฅ

3. cdk synth

- stack์— ์•„๋ž˜ ๊ตฌ๋ฌธ ์ถ”๊ฐ€(S3 bucket ์ƒ์„ฑ)

- scope : Tells the bucket that the stack is its parent: it is defined within the scope of the stack. You can define constructs inside of constructs, creating a hierarchy (tree).

- id : The logical ID of the Bucket within your AWS CDK app. This (plus a hash based on the bucket's location within the stack) uniquely identifies the bucket across deployments so the AWS CDK can update it if you change how it's defined in your app. Buckets can also have a name, which is separate from this ID (it's the bucketName property).

- props : Whether this bucket should have versioning turned on or not. Default: false

 

Bucket — AWS Cloud Development Kit 1.94.1 documentation

noncurrent_version_expiration (Optional[Duration]) – Time between when a new version of the object is uploaded to the bucket and when old versions of the object expire. For buckets with versioning enabled (or suspended), specifies the time, in days, betw

docs.aws.amazon.com

from aws_cdk import aws_s3 as s3

        bucket = s3.Bucket(self,
                           "eunhye_bucktet_0225",
                           versioned=True)

- ์‚ฌ์šฉํ•  AWS ์„œ๋น„์Šค ์„ค์น˜

- ์Šคํƒ์„ CloudFormation template์œผ๋กœ ๋ณ€ํ™˜ ํ›„ ์ถœ๋ ฅ

4. cdk deploy

- ์Šคํƒ์„ AWS(config ์„ค์ •๋œ AWS ๊ณ„์ •)๋กœ ๋ฐฐํฌ

- AWS Console - CloudFormation์—์„œ ๋ฐฐํฌ๋œ ์Šคํƒ ํ™•์ธ ๊ฐ€๋Šฅ

- AWS Console - S3์—์„œ CdkStack์œผ๋กœ ์ƒ์„ฑ๋œ bucket ํ™•์ธ ๊ฐ€๋Šฅ

* bucket name์€ ์˜์–ด ์†Œ๋ฌธ์ž์™€ ํ•˜์ดํ”ˆ(-)๋งŒ ํ—ˆ์šฉํ•˜์—ฌ  cdk stack์— ์ž‘์„ฑํ•œ "eunhye_bucktet_0225"์œผ๋กœ bucket name์ด ์ง€์ •๋˜์ง€ ์•Š์Œ

5. cdk diff

- stack์— ์•„๋ž˜ ๊ตฌ๋ฌธ ๋ณ€๊ฒฝ

- removal_policy : Policy to apply when the bucket is removed from this stack. Default: - The bucket will be orphaned.

- auto_delete_objects : Whether all objects should be automatically deleted when the bucket is removed from the stack or when the stack is deleted. Requires the removalPolicy to be set to RemovalPolicy.DESTROY. Default: false

        bucket = s3.Bucket(self,
                           "eunhye_bucktet_0225",
                           versioned=True,
                           removal_policy = core.RemovalPolicy.DESTROY,
                           auto_delete_objects = True)

- ๋ฐฐํฌ๋œ ์Šคํƒ๊ณผ ํ˜„์žฌ ์Šคํƒ์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ ํ™•์ธ ๊ฐ€๋Šฅ

6. cdk bootstrap

- cdk toolkit stack์„ ์ƒ์„ฑํ•˜์—ฌ cdk deploy ์ „ ํ•„์š”

- staging area ์—ญํ• 

- ์Šคํƒ์„ AWS๋กœ ๋ฐฐํฌ

- AWS Console - CloudFormation์—์„œ ๋ฐฐํฌ๋œ ์Šคํƒ ํ™•์ธ ๊ฐ€๋Šฅ

- AWS Console - S3์—์„œ CDKToolkit์œผ๋กœ ์ƒ์„ฑ๋œ bucket ํ™•์ธ ๊ฐ€๋Šฅ

- CloudFormation์—์„œ Designer๋กœ ๋ณด๊ธฐ

7. cdk destroy

- cdk stack(๋ฐฐํฌ๋œ CloudFromation๊ณผ AWS Resource) ์‚ญ์ œ

- AWS Console - S3์—์„œ ์‚ญ์ œ๋œ bucket ํ™•์ธ ๊ฐ€๋Šฅ

'Devops' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

terraform ๋ฒ„์ „ ๊ด€๋ฆฌ : tfenv  (0) 2021.10.03
Config ์ž๋™ํ™” ํˆด(puppet, chef, salt, ansible)  (0) 2021.04.13
Terraform ์‚ฌ์šฉ๋ฒ•  (0) 2021.03.20
Infrastructure as Code(IaC)  (0) 2021.03.20
Git ์‚ฌ์šฉ๋ฒ•  (0) 2021.01.11