Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AWS CDKを触ってみて/Try the AWS CDK

AWS CDKを触ってみて/Try the AWS CDK

Toru_Kubota

July 22, 2022
Tweet

More Decks by Toru_Kubota

Other Decks in Technology

Transcript

  1. AWS CDKを触ってみて NCDC Dev Meetup クラウドなんでもLT会 #6 2022/07/22 久保田 亨

  2. 自己紹介 氏名 : 久保田 亨 会社 : SaaS企業 職業 : インフラエンジニア 趣味 : 歩くこと

  3. 背景 ~2022/06 SI勤務(DC) 2022/07~(転職) SaaS企業 ・AWSサービス検証 ・AWSサービスメニュー化 ・AWSを使用 CloudFormation(CFn) CloudFormation(CFn)

  4. CFn (CloudFormation) Stack Template YAML/JSON AWS Cloud CloudFormation(CFn)

  5. 実行環境 AWS CDK Stack Template JavaScript/TypeScript/Python/Java/C# AWS Cloud AWS CDK

  6. AWS Cloud VPC Public subnet Public subnet Private subnet Private

    subnet AWS Cloud VPC Public subnet Public subnet Private subnet Private subnet AWS Cloud VPC Public subnet Public subnet Private subnet Private subnet AWS Cloud VPC Public subnet Public subnet Private subnet Private subnet AWS Cloud VPC Public subnet Public subnet Private subnet Private subnet
  7. import { Stack, StackProps } from 'aws-cdk-lib'; import { Construct

    } from 'constructs'; import * as ec2 from 'aws-cdk-lib/aws-ec2'; import { Vpc } from 'aws-cdk-lib/aws-ec2'; export class VpcStack extends Stack { //別スタックから参照できるようにする public readonly vpc: Vpc; constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); const vpc_name = 'cdk-vpc'; const cidr = '10.1.0.0/16'; this.vpc = new ec2.Vpc(this, 'VPC', { vpcName: vpc_name, cidr: cidr, maxAzs: 2, subnetConfiguration: [ { cidrMask: 24, name: `${vpc_name}-public`, subnetType: ec2.SubnetType.PUBLIC, }, { cidrMask: 24, name: `${vpc_name}-private`, subnetType: ec2.SubnetType.PRIVATE_ISOLATED, }, ] }) } };
  8. Resources: VPCXXXXXXX: Type: 'AWS::EC2::VPC' Properties: CidrBlock: 10.1.0.0/16 EnableDnsHostnames: true EnableDnsSupport:

    true InstanceTenancy: default Tags: - Key: Name Value: cdk-vpc Metadata: 'aws:cdk:path': VpcStack/VPC/Resource VPCcdkvpcpublicSubnet1SubnetYYYYYYYY: Type: 'AWS::EC2::Subnet' Properties: VpcId: !Ref VPCXXXXXXX AvailabilityZone: ap-northeast-1a CidrBlock: 10.1.0.0/24 MapPublicIpOnLaunch: true Tags: - Key: 'aws-cdk:subnet-name' Value: cdk-vpc-public - Key: 'aws-cdk:subnet-type' Value: Public - Key: Name Value: VpcStack/VPC/cdk-vpc-publicSubnet1 Metadata: 'aws:cdk:path': VpcStack/VPC/cdk-vpc- publicSubnet1/Subnet VPCcdkvpcpublicSubnet1RouteTableZZZZZZZZ: Type: 'AWS::EC2::RouteTable' Properties: VpcId: !Ref VPCXXXXXXXX Tags: - Key: Name Value: VpcStack/VPC/cdk-vpc-publicSubnet1 ・・・・222行(CDKで出力されたテンプレート)
  9. https://qiita.com/Toru_Kubota/items/f6fb42c5da442b03a98f ALB+ECS+Aurora

  10. //aws-ecs-patternsモジュール使用 new ecsp.ApplicationLoadBalancedFargateService(this, 'WebApp01', { cluster, domainName: domain_name, domainZone, memoryLimitMiB:

    512, desiredCount: 2, cpu: 256, assignPublicIp: true, loadBalancerName: 'WebApp01-lb01', publicLoadBalancer: true, taskImageOptions: { image: ecs.ContainerImage.fromRegistry('nginx'), },}); ALB+Fargate設定(一部抜粋)
  11. ・セキュアなベースラインを提供するテンプレート ・AWS CDKで実装 ・共通設定とシステム固有設定の2層 Baseline Environment on AWS

  12. 共通設定箇所 Amazon GuardDuty AWS Security Hub AWS CloudTrail AWS Config

    Amazon SNS AWS Systems Manager
  13. ・少ないコード量で運用が楽そう! ・書き方は慣れれば難しくない! ・BLEAと合わせて運用が楽になりそう! AWS CDKを触ってみて