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

aws-dev-day-2020-f9-cdk-bestpractice

 aws-dev-day-2020-f9-cdk-bestpractice

Hiroyoshi HOUCHI

October 21, 2020
Tweet

More Decks by Hiroyoshi HOUCHI

Other Decks in Technology

Transcript

  1. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with 2 0 - 2 2 . 1 0 . 2 0 2 0
  2. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with CDK    @hixi-hyi F - 9
  3. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with T?R/ E<=2 @hixi-hyi 2013@HB1P-,#,-G93P -&+&( %-'-$!+"  :JI0 &),(!- SY N;P4KF+"WX C> 7J6A5ODV+*,- ,+%( 7JM8. Cloudformation 5O  CDK ))-L CDK QU
  4. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with ,! 4#% • 3$   (-)4 • "02!  .651  7*/ • !#%8. !   +'!&
  5. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with CDK    • id  •   
  6. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with
  7. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with cdk init      cdk init app --language=typescript aws-cdk-bestpractice
  8. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with cdk init      cdk init app --language=typescript aws-cdk-bestpractice bin/aws-cdk-bestpractice.ts const app = new cdk.App(); new AwsCdkBestpracticeStack(app, 'AwsCdkBestpracticeStack’);
  9. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with cdk init      cdk init app --language=typescript aws-cdk-bestpractice bin/aws-cdk-bestpractice.ts const app = new cdk.App(); new AwsCdkBestpracticeStack(app, 'AwsCdkBestpracticeStack’); lib/aws-cdk-bestpractice.ts export class AwsCdkBestpracticeStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, …) { super(scope, id, props); } }
  10. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with cdk init      cdk init app --language=typescript aws-cdk-bestpractice bin/aws-cdk-bestpractice.ts const app = new cdk.App(); new AwsCdkBestpracticeStack(app, 'AwsCdkBestpracticeStack’); lib/aws-cdk-bestpractice.ts export class AwsCdkBestpracticeStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, …) { super(scope, id, props); } }
  11. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with !  • VOHN%2T/(0&'*+%AS   VOHN,.-0)%1 U4" • /(0&ASW"PB human readable ;9%  /(0&K F?5%JD   construct 37MCE$#" I<I<F?5%>U K " "LX /(0&F?8R@= " #%1 6G "Q:
  12. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with id  -  const id = new cdkid.Identifier({ section: resolveEnvironmentFromAwsAccountId(), }); const app = new cdk.App(); id.child({legion: "product"}).scope((id: cdkid.Identifier) => { id.child({cohort: "service"}).scope((id: cdkid.Identifier) => { new stack.ProductServiceApi(app, id.child({family: 'api'})); }); }); export class ProductServiceApi extends cdk.Stack { constructor(app: cdk.App, id: cdkid.Identifier, props: ProductServiceApiProps) { ... } } https://github.com/hixi-hyi/aws-cdk-identifier
  13. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with id   -  export class ProductServiceApi extends cdk.Stack { constructor(app: cdk.App, id: cdkid.Identifier, props: ProductServiceApiProps) { super(app, id.stackName, props); const fargate = new construct.Fargate(this, id.child("Aurora"), { maxCapacity: cdkid.Variables.resolve(id, cdkid.RankLoc.Section, { dev: 1, prod: 10, }), }); }    https://github.com/hixi-hyi/aws-cdk-identifier
  14. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with id   -  export class Fargate extends cdk.Construct { constructor(app: cdk.Construct, id: cdkid.Identifier, props: FargateProps) { super(app, id.constructName); const cluster = new ecs.Cluster(this, 'Cluster’, { clusterName: id.camelName, ... }); ... } https://github.com/hixi-hyi/aws-cdk-identifier Construct  
  15. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with id  - +α  const patterns = new ecsPatterns.ApplicationLoadBalancedFargateService(this, “Service”, { ... taskImageOptions: { environment: { ‘IAC_CORE_SECTION’: id.rank.section, // dev or prod }, }, }); https://github.com/hixi-hyi/aws-cdk-identifier 
  16. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with id 
  17. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with
  18. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with   • #!"$ •   !
  19. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with  &/) • 7' 13(4%-.8 • *0#2 $,13( !"56  •    •  +
  20. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with  ├── bin │ └── infra.ts └── lib ├── @aws-cdk/ ├── construct/ ├── helper/ └── stack/
  21. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with &# ├── bin │ └── infra.ts (lib/stack   stack ! "+ ) └── lib ├── @aws-cdk (cdk  %  ) ) ├── construct (,) L2/patterns construct "+ ) ├── helper ( ('* $ "+ ) └── stack (lib/construct   ! "+ )
  22. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with
  23. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with bin/infra.ts  id = cdkid.Identifier({ section: resolveEnvironmentFromAwsAccountId() }); const common = new stack.ProductCommon(app, id); new stack.ProductServiceApi(app, id.child({cohort: “service”, family: 'api'}), { common });
  24. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with
  25. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with lib/stack/*  export class ProductServiceApi extends cdk.Stack { constructor(app: cdk.App, id: cdkid.Identifier, props: ProductServiceApiProps) { super(app, id.stackName, props); }
  26. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with lib/stack/*  export class ProductServiceApi extends cdk.Stack { constructor(app: cdk.App, id: cdkid.Identifier, props: ProductServiceApiProps) { super(app, id.stackName, props); const ecr = new construct.Ecr(this, id.child('Repository’)); }
  27. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with lib/stack/*  export class ProductServiceApi extends cdk.Stack { constructor(app: cdk.App, id: cdkid.Identifier, props: ProductServiceApiProps) { super(app, id.stackName, props); const ecr = new construct.Ecr(this, id.child('Repository’)); const fargate = new construct.FargateL7Webapp(this, id.child('Webapp'), { ... image: ecs.ContainerImage.fromEcrRepository(ecr.repository), maxCapacity: cdkid.Variables.resolve(id, cdkid.RankLoc.Section, { dev : 1, prod: 10, }), }); ... }
  28. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with lib/stack/*  export class ProductServiceApi extends cdk.Stack { constructor(app: cdk.App, id: cdkid.Identifier, props: ProductServiceApiProps) { super(app, id.stackName, props); const ecr = new construct.Ecr(this, id.child('Repository’)); const fargate = new construct.FargateL7Webapp(this, id.child('Webapp'), { ... image: ecs.ContainerImage.fromEcrRepository(ecr.repository), maxCapacity: cdkid.Variables.resolve(id, cdkid.RankLoc.Section, { dev : 1, prod: 10, }), }); ... }
  29. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with
  30. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with lib/construct/* 
  31. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with
  32. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with lib/@aws-cdk/*  https://github.com/aws/aws-cdk/issues/9926 https://github.com/aws/aws-cdk/pull/10324
  33. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with
  34. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with lib/helper/*  export function LogGroupRetentionDay(id: cdkid.Identifier): logs.RetentionDays { return cdkid.Variables.resolve<logs.RetentionDays>(id, cdkid.RankLoc.Section, { dev: logs.RetentionDays.TWO_WEEKS, prod: logs.RetentionDays.INFINITE, }) }
  35. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with *'&1 ├── bin │ └── infra.ts (lib/stack !  stack $ %/ ) └── lib ├── @aws-cdk (cdk #)  - ) ├── construct (0- L2/patterns construct %/ ) ├── helper ( ,"+. #( %/ ) └── stack (lib/construct !  $ %/ )
  36. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with
  37. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with   • id )-$ • )& %",# (' )&* !  +   https://github.com/hixi-hyi/aws-cdk-bestpractice
  38. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with
  39. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with
  40. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with Q1. L1   L2   L2  
  41. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with Q1. L1 + L2 +   *% &"$ # ) !'  lib/@aws-cdk ( PR  
  42. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with
  43. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with Q2.   
  44. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with Q1.       cdk diff    
  45. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with
  46. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. In Partnership with Q3. IaC ,9 814)* )+3  !%02&'7  • 81-%8145/  IAM Policy .'  • SSM Parameters  /service/xxx/app/* #"8145/6$( • !81 Dockerfile 
  47. Thank you! © 2020, Amazon Web Services, Inc. or its

    affiliates. All rights reserved. In Partnership with   CDK   • id  •    https://github.com/hixi-hyi/aws-cdk-bestpractice