๐ ๋ชฉ์ฐจ
1. AWS & CDK ์ค์
AWS Configure
๊ธฐ์กด์ aws configure๋ฅผ ํตํด AWS์ ๋ก์ปฌ์ด ์ฐ๊ฒฐ๋์ด ์๋ค๋ฉด ๊ฑด๋๋ฐ์ด๋ ๋ฉ๋๋ค.
aws configure list
- AWS์ IAM์์ ํคํ์ด(AWS Access Key, AWS Secret Access Key) ๋ฐ๊ธ
์ฐธ๊ณ ) ํคํ์ด ๋ฐ๊ธ https://docs.aws.amazon.com/ko_kr/cli/latest/userguide/cli-configure-quickstart.html#cli-configure-quickstart-creds - terminal์์ aws configure ์ปค๋งจ๋๋ฅผ ํตํด aws ๊ณ์ ์ฐ๊ฒฐ
$ aws configure
AWS Access Key ID: [aws ์์ IAM์์ฑํ์ฌ ์
๋ ฅ]
AWS Secret Access Key : [aws ์์ IAM์์ฑํ์ฌ ์
๋ ฅ]
Default region name [None]: ap-northeast-2
Default output format [None]: json
aws-cdk ์ค์น
- npm์ ํตํด aws-cdk ์ค์น
$ sudo npm i -g aws-cdk
$ cdk version # 2.15.0 (build 151055e)
2. CDK ํ๋ก์ ํธ ์์ฑ
cdk init
- cdk init ์ปค๋งจ๋๋ก cdkํ๋ก์ ํธ๊ฐ ์์ฑ
$ cdk init sample-app --language typescript
vsCode๋ก sample-app ํ๋ก์ ํธ๋ฅผ ์ด์ด๋ณด๋ฉด ํ์ผ ๊ตฌ์กฐ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
3. ์คํ์ lambda, API GW ์ถ๊ฐ
์คํ์ด๊ธฐํ
lib ๋๋ ํ ๋ฆฌ๋ฅผ ๋ณด๋ฉด ์์ฑ๋์ด ์๋ stack์ด ์์ต๋๋ค.
sample-app์ ํตํด ํ๋ก์ ํธ๋ฅผ ์์ฑํ์๊ธฐ์ queue ๋ฑ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ค์ด๊ฐ์๋ ์ฝ๋๋ค์ด ์์ํ ๋ฐ,
์๋ ์๋ ์ฝ๋๋ฅผ ๋ชจ๋ ๋ ๋ฆฌ๊ณ ์๋์ ๊ฐ์ด ๋ง๋ค์ด์ค๋๋ค.
// lib/sample_cdk-stack.ts
import { Duration, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
export class SampleCdkStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
}
}
๋๋ค ํจ์ ๋ง๋ค๊ธฐ
- root๋๋ ํ ๋ฆฌ์ (bin, lib์ ๊ฐ์ ๊ณ์ธต์) lambda ๋๋ ํ ๋ฆฌ ์์ฑ
- lambda ๋๋ ํ ๋ฆฌ ๋ด hello.js ํ์ผ ์์ฑ
- hello.js์ ์๋์ ๊ฐ์ด ์์ฑ
exports.handler = async function(event) {
console.log('requests: ', JSON.stringify(event, undefined, 2))
return {
statusCode: 200,
headers: { 'Content-Type' : 'text/plain' },
body: `hello! ${event.path}\n`
}
}
lambda contruct library ์ค์น
- ์๋ ์ปค๋งจ๋๋ก lambda construct libaray ์ค์น
- ์ด๊ฒ ์ค์น๋์ด ์์ด์ผ ์์ฑํ lambda ์ฝ๋๋ฅผ cdk์์ ๋ถ๋ฌ์ฌ ์ ์์
$ npm install @aws-cdk/aws-lambda
stack์ ๋๋ค ์ ์
stack์ด ์ ์๋์ด ์๋ ํ์ผ(lib/~~.stack.ts)์ lambda๋ฅผ ์ ์ํด์ค๋๋ค.
// lib/sample_cdk-stack.ts
import { Duration, Stack, StackProps } from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { Construct } from 'constructs';
export class SampleCdkStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
// defines an AWS Lambda resource
const hello = new lambda.Function(this, 'HelloHandler', {
runtime: lambda.Runtime.NODEJS_14_X, // execution environment
code: lambda.Code.fromAsset('lambda'), // code loaded from "lambda" directory
handler: 'hello.handler' // file is "hello", function is "handler"
});
}
}
stack์ api gateway ์ถ๊ฐ ์ ์
api gateway ๋ stack ์ ์ถ๊ฐํด์ค๋๋ค.
// lib/sample_cdk-stack.ts
import { Duration, Stack, StackProps } from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as apigw from 'aws-cdk-lib/aws-apigateway';
import { Construct } from 'constructs';
export class SampleCdkStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
// defines an AWS Lambda resource
const hello = new lambda.Function(this, 'HelloHandler', {
runtime: lambda.Runtime.NODEJS_14_X, // execution environment
code: lambda.Code.fromAsset('lambda'), // code loaded from "lambda" directory
handler: 'hello.handler' // file is "hello", function is "handler"
});
// defines an API Gateway REST API resource backed by our "hello" function.
new apigw.LambdaRestApi(this, 'Endpoint', {
handler: hello
});
}
}
4. AWS์ ๋ฐฐํฌ
bootstrap stack ์ค์น (cdk bootstrap)
AWS CDK ์ฑ์ ์ฒ์์ผ๋ก ๋ํด๋ก์ด ํ ๋๋, "bootstrap stack์ ์ค์นํด์ฃผ์ด์ผ ํฉ๋๋ค.
- bootstrap stack ์ค์น
$ cdk bootstrap
cdk synth
cdk synth ์ปค๋งจ๋๋ cdkํ๋ก์ ํธ ์ฝ๋๋ฅผ cloudFormation ํ ํ๋ฆฟ์ผ๋ก ๋ณํํด์ค๋๋ค.
- cdk synth ์ปค๋งจ๋๋ฅผ ํตํด cloudFormation์ด ์ด๋ป๊ฒ ๊ตฌ์ฑ๋ ์ง ํ์ธํ ์ ์์
- deploy๋ฅผ ์ํด ๊ผญ ํ์ํ ๋จ๊ณ๋ ์๋
$ cdk synth
Resources:
CdkworkshopQueue18864164:
Type: AWS::SQS::Queue
Properties:
VisibilityTimeout: 300
Metadata:
aws:cdk:path: cdkworkshop/CdkworkshopQueue/Resource
CdkworkshopQueuePolicy78D5BF45:
Type: AWS::SQS::QueuePolicy
Properties:
PolicyDocument:
Statement:
- Action: sqs:SendMessage
Condition:
ArnEquals:
aws:SourceArn:
Ref: CdkworkshopTopic58CFDD3D
Effect: Allow
Principal:
Service: sns.amazonaws.com
Resource:
Fn::GetAtt:
- CdkworkshopQueue18864164
- Arn
Version: "2012-10-17"
Queues:
- Ref: CdkworkshopQueue18864164
Metadata:
aws:cdk:path: cdkworkshop/CdkworkshopQueue/Policy/Resource
CdkworkshopQueuecdkworkshopCdkworkshopTopic7642CC2FCF70B637:
Type: AWS::SNS::Subscription
Properties:
Protocol: sqs
TopicArn:
Ref: CdkworkshopTopic58CFDD3D
Endpoint:
Fn::GetAtt:
- CdkworkshopQueue18864164
- Arn
Metadata:
aws:cdk:path: cdkworkshop/CdkworkshopQueue/cdkworkshopCdkworkshopTopic7642CC2F/Resource
CdkworkshopTopic58CFDD3D:
Type: AWS::SNS::Topic
Metadata:
aws:cdk:path: cdkworkshop/CdkworkshopTopic/Resource
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Modules: aws-cdk=1.18.0,jsii-runtime=Python/3.7.3
cdk deploy
cdk deploy ์ปค๋งจ๋๋ฅผ ์คํํ๋ฉด ํ์ฌ๊น์ง ์์ฑํ ์คํ์ด aws์ ๋ฐฐํฌ๋ฉ๋๋ค.
$ cdk deploy
๋ํ๋ก์ด๊ฐ ์๋ฃ๋๋ฉด ์์ฒ๋ผ api gateway url์ ๋ณด์ฌ์ค๋๋ค. ์ด url๋ก ๋ค์ด๊ฐ๋ณด๋ฉด api gateway -> lambda๋ฅผ ํตํด response๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
AWS ๊ณ์ ์์๋ ๋ฆฌ์์ค๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
AWS > cloudFormation์ ๊ฐ๋ณด๋ ์ ๋ค์ด๊ฐ ์๋ค์
AWS > Lambda์๋ hello handler๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
ํน์ ๋ํ๋ก์ด๊ฐ ์ ์์ ์ผ๋ก ๋์๋๋ฐ, ๊ณ์ ์์ ์ฐพ์ ์ ์๋ค๋ฉด,
- ๋ค๋ฅธ ๊ณ์ ๊ณผ ์ฐ๊ฒฐ๋์๋์ง ํ์ธํด๋ณด์ธ์
- ๋ค๋ฅธ ๋ฆฌ์ ์ ๋ฐฐํฌํ์๋์ง ํ์ธํด๋ณด์ธ์
aws configure list๋ฅผ ํตํด ๋ํดํธ๋ก ์ค์ ๋ aws ๊ณ์ ๋ฐ ๋ฆฌ์ ์ ํ์ธํ ์ ์์ต๋๋ค.
์ฐธ๊ณ )
https://towardsthecloud.com/how-to-set-up-aws-cdk
https://cdkworkshop.com/20-typescript/20-create-project/500-deploy.html