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

AWS Serverless Application Modelのデプロイ戦略

AWS Serverless Application Modelのデプロイ戦略

Developers.IO 2017の発表スライドです。

Koji Nakayama

July 03, 2017
Tweet

More Decks by Koji Nakayama

Other Decks in Technology

Transcript

  1. ᛔ૩奧Օ • Ӿઊ ଛလ • μ϶φϮϊϐϖ AWSԪ䮣᮱ ϊϷϲЄτϴ ЀίЄκϓμϕ •

    AWSΨڥአͭ͵αЀϢ϶΄戔懯/䯤塈/π ЀςϸϓΰЀν • GitHub: knakayama • 奺䵉 • ηЀϤϹςЄϝ΄晁አ3ଙ • AWS䵉 1ଙ܎ͦΟ͚ 2
  2. AWS Serverless Application Model΄༷ᥝ • ςЄϝϹφίЄκϓμώϰΨᓕቘͯΡ͵Η΄ϯϔϸ • ኼͭͼAWS SAM;޷Ά΢Ρͩ;͢ग़͚ •

    Lambda΀Ϳ΄AWSςЄϠφΨਯ᥺ጱ΀πЄϖ;ͭͼᓕቘݢᚆ • → ίЄκϓμώϰق֛ΨπЄϖͽᓕቘͽͣΡ • → Git/GitHub;昧൭ͭͼCI/CDϞαϤ϶αЀΨ֢΢Ρ • 䋚䙪΅CloudFormation΄䝭䔴䱛ᚆ 5
  3. AWS SAM΄ϓЀϤϹЄϕֺ --- AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::Serverless-2016-10-31 Description: AWS SAM

    Sample Resources: Func: Type: AWS::Serverless::Function Properties: CodeUri: path/to/func Handler: index.handler Runtime: nodejs6.10 Events: Get: Type: Api Properties: Path: /users/{id} Method: get RestApiId: !Ref Api Api: Type: AWS::Serverless::Api ... 6
  4. AWS SAM΄ϓЀϤϹЄϕֺ --- AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::Serverless-2016-10-31 # AWS SAM

    ΨڥአͯΡͩ;Ψกᐏጱ΁೰ਧ Description: AWS SAM Sample Resources: Func: Type: AWS::Serverless::Function Properties: CodeUri: path/to/func Handler: index.handler Runtime: nodejs6.10 7
  5. AWS SAM΄ϓЀϤϹЄϕֺ --- AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::Serverless-2016-10-31 Description: AWS SAM

    Sample Resources: Func: Type: AWS::Serverless::Function # Lambda አϷϊЄφ΄ਧ嬝 Properties: CodeUri: path/to/func Handler: index.handler Runtime: nodejs6.10 8
  6. AWS SAM΄ϓЀϤϹЄϕֺ --- AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::Serverless-2016-10-31 Description: AWS SAM

    Sample Resources: Func: Type: AWS::Serverless::Function Properties: CodeUri: path/to/func # Lambda ΄ϊЄφπЄϖΨތΖϔΰϹμϕϷΎ΄Ϟφ Handler: index.handler Runtime: nodejs6.10 9
  7. AWS SAM΄ϓЀϤϹЄϕֺ --- AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::Serverless-2016-10-31 Description: AWS SAM

    Sample Resources: Func: Type: AWS::Serverless::Function Properties: CodeUri: path/to/func Handler: index.handler # ϜЀϖ϶΄೰ਧ (< Ϣήαϸݷ >.< 樛හݷ >) Runtime: nodejs6.10 10
  8. AWS SAM΄ϓЀϤϹЄϕֺ --- AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::Serverless-2016-10-31 Description: AWS SAM

    Sample Resources: Func: Type: AWS::Serverless::Function Properties: CodeUri: path/to/func Handler: index.handler Runtime: nodejs6.10 # ϶Ѐόαϭ΄೰ਧ 11
  9. AWS SAM΄ϓЀϤϹЄϕֺ ... Func: Type: AWS::Serverless::Function ... Events: Get: Type:

    Api # API Gateway;΄αЀϓνϹЄτϴЀ Properties: Path: /users/{id} Method: get RestApiId: !Ref Api Api: Type: AWS::Serverless::Api # API GatewayϷϊЄφ΄ਧ嬝 ... 12
  10. AWS SAM;CLI • ϺЄθϸ΄ϊЄφπЄϖ͡ΟϔϤϺαϮЀϕϞϐξЄυ΄֢౮/ίϐϤϺЄϖ͢ͽͣ Ρ • → ӞଶS3΁ίϐϤϺЄϖͭͼ͡ΟAWS SAM΄ϓЀϤϹЄϕΨ䄜䟵ͯΡ;͚͜㵕 ֢

    • → ֺ͞Ά CodeUri ϤϺϞϓΰΨS3Ύ΄Ϟφ΁䄜䟵ͭͼͥ΢Ρ • 䌑አ΄πϫЀϖ΅አ఺ͫ΢ͼ΀͚΄ͽAWS CLIΨڥአͯΡ • → 㷗ੂ΁᥺͜;ϢϹЄϭϼЄμͽ΅΀ͥͥ͘Δͽϯϔϸ;͚֖͜ᗝͻͧ΄͵Η • → Serverless FrameworkΚApex;΅吖΀ΡϪαЀϕ 13
  11. 䄜䟵ͫ΢͵AWS SAM΄ϓЀϤϹЄϕֺ --- AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::Serverless-2016-10-31 Description: AWS SAM

    Sample Resources: Func: Type: AWS::Serverless::Function Properties: CodeUri: s3://<_S3_BUCKET_>/<_S3_PREFIX_>/<DEPLOYMENT_PACKAGE> Handler: index.handler Runtime: nodejs6.10 14
  12. AWS SAM΄ϔϤϺα $ aws cloudformation package \ --template-file sam.yml \

    --s3-bucket <_S3_BUCKET_> \ --s3-prefix <_S3_PREFIX_> \ --output-template-file .sam/packaged.yml $ aws cloudformation deploy \ --template-file .sam/packaged.yml \ --stack-name <_STACK_NAME_> \ --capabilities CAPABILITY_IAM 16
  13. ๋֗3ͺ΄厏हΨአ఺ͯΡ • Develop厏ह • → ϺЄθϸ΄樄咲厏ह • Staging厏ह • →

    αЀϓνϹЄτϴЀϓφϕΨ䋚ෞͯΡ厏ह • → च๜ጱ΁Production;ݶᒵ΄厏ह΁ͭͼͥ͠Ώͣ • Production厏ह • → ͠ਮͫΩ͢䋚檭΁ڥአͯΡ厏ह 18
  14. ϣ϶Ѐώ䜐ኼ ϣ϶Ѐώݷ አ᭔ master ώδϐμίγϕز;΀Ρϣ϶Ѐώ ձ఺( add-new-feature ;͡) master͡Οώδϐμίγϕͭ͵ϕϡϐμ ϣ϶Ѐώ

    staging stagingአϷϷЄφϣ϶Ѐώ Protected Branch production productionአϷϷЄφϣ϶Ѐώ Protected Branch 23
  15. ϣ϶Ѐώ΄϶αϢςαμϸ 1. master ͡Οϕϡϐμϣ϶Ѐώ֢౮ 2. master ΁䌏ͭͼWIP PRڊͯ 3. 樄咲

    4. master ΁PR΄ϹϠϲЄׁ毲&ϫЄυ 5. ϓφϕ΄όαϬЀνͽ master ͡Ο staging ΁PR & ϫЄυ 6. staging ͽαЀϓνϹЄτϴЀϓφϕ 7. ϷϷЄφ΄όαϬЀνͽ staging ͡Ο production ΁PR & ϫЄυ 24
  16. ϔϤϺαϮЀϕϞϐξЄυΨੜͫͥͯΡ • ϔϤϺαϮЀϕϞϐξЄυ΅஠ᥝ΀Θ΄զक़ތΔ΀͚ • → Lambda΄πЄϸϖφόЄϕΨᎨ婘ͯΡ͵Η • AWS SAM΅ϔϤϺαϮЀϕϞϐξЄυ΄ݐ䞳晝䝑͢୧͚΄ͽϓφϕ πЄϖΘړ櫝

    • → ͺΔΠLambda΄ϊЄφπЄϖ;ݶͮϔΰϹμϕϷ΁΅ތΗ΀͚ • → Serverless Framework΅ϼαϸϖθЄϖͽຨ敟΁晝䝑ͽͣΡ΄Ͷ ͢… https://speakerdeck.com/keisuke69/quan-bu-jiao-emasu-saharesuahurifalseantihatantotiyuninku?slide=24 26
  17. 1ͺ΄πЄϖϦЄφ;愢හ厏हΎ΄䌏䖕 • 厏ह䶅΁φόϐμΨړ櫝ͯΡ • → AWS SAM΅匍䦒ᅩͽLambda΄Version & Alias;ፘ௔䘂͚ •

    → aws cloudformation deploy ΄ --stack-name ͽ厏ह䶅΁φόϐμΨړ櫝ͯΡ • 厏ह䶅΁吖΀ΡϞ϶ϮЄόΨ戔ਧϢήαϸͽᓕቘ • → params զӥ΁厏ह䶅΄Ϟ϶ϮЄόΨތΩͶ戔ਧϢήαϸΨአ఺ • → aws cloudformation deploy ΄ --parameter-overrides ͽ厏ह䶅΄Ϟ϶ϮЄ όΨޕ㷧 27
  18. ϷϪυϕϷͽਠ奾ͫͱΡ • ϷϪυϕϷ΁قͼ΄ఘ䁭ΨތΗΡ • → 愢හՈͽ΄樄咲ͽΘݶͮ厏हΨͯͦ΁ٚ匍ͽͣΡΞ͜΁ͭͼͥ͠ • → package.json΁஠ᥝ΀ϯυϲЄϸΨقͼف΢Ρ •

    AWS CLI΄϶ϐϞЄφμϷϤϕΨአ఺ͭͼͥ͠ • → AWS SAMΨڥአͭͼ͚Ρ;AWS CLIΨ毱ᔺ΁ڥአͯΡ͵Η • → ϶ϐϞЄφμϷϤϕΨ bin զӥ΁ᯈᗝͭͼ package.json ΄ scripts ͡Ο޷Ήڊͯ • → npm or yarn ͽ䋚ᤈ • → AWS CLI΄ϝЄυϴЀΘ requirements.txt ͽᓕቘ 28
  19. 㰜͢ᘍ͞͵๋䔶΄ϔΰϹμϕϷ䯤᭜ ϔΰϹμϕϷ or Ϣήαϸݷ ஀ۆ .sam/ aws cloudformation package ͽ䄜䟵ͫ΢͵ϓЀϤ

    ϹЄϕ( .sam/packaged.yml ΀Ϳ) ݶͮϢήαϸΨٚኞ౮ݢᚆ΀΄ͽ .gitignore ͽᴻक़ bin/ AWS CLI΄϶ϐϞЄφμϷϤϕ( bin/deploy.sh ΀Ϳ) package.json ϯυϲЄϸ;϶ϐϞЄφμϷϤϕ΄ᓕቘ params/ 厏ह䶅΄Ϟ϶ϮЄό( params/stg.json ΀Ϳ) requirements.txt AWS CLI΄ϝЄυϴЀᓕቘ sam.yml AWS SAM΄ϓЀϤϹЄϕ 29
  20. 㰜͢ᘍ͞͵๋䔶΄ϔΰϹμϕϷ䯤᭜ ϔΰϹμϕϷ or Ϣήαϸݷ ஀ۆ src/ Lambda΄ϊЄφπЄϖΚAPI Gateway΄SwaggerϢήα ϸᗝͣ䁰 sam.yml

    ;΅吖΀ΡϓЀϤϹЄϕΨڥአͭ͵͚䁰ݳ΅ͩ ͩ΁ᗝͥ΄Θ͘Π src/api/swagger.yml API Gateway΄SwaggerϢήαϸ(ֵ͜䁰ݳ) src/handlers/<樛හݷ>/ Lambda΄ϊЄφπЄϖᗝͣ䁰 test/ ϓφϕπЄϖᗝͣ䁰( test/<樛හݷ>.spec.js ΀Ϳ) yarn.lock yarn΄ϺϐμϢήαϸ(ֵ͜䁰ݳ) buildspec.yml CodeBuild΄戔ਧϢήαϸ 30
  21. AWS SAM;CI/CD • AWS SAMͽCI/CDͯΡ͵Η΄ϑЄϸ or ςЄϠφ΅͚Σ͚Σ͘Ρ • → Jenkins/CircleCI/Travis

    CI/etc... • Քࢧ΅CodePipeline;CodeBuild΄奲ΕݳΥͱΨͪ奧Օ • → ݱ圵AWSςЄϠφ;΄昧൭͢ಋܹ͚ܦ᨝ • ͵Ͷ̵ͭΚΣ͜;ͭͼ͚Ρٖ਻ጱ΁Ϳ΄ϑЄϸ or ςЄϠφͽΘ ͽͣΡ΄ͽ֜ͽΘΞ͚΄ͽ΅ 32
  22. CodePipeline • CI/CD΄ϞαϤ϶αЀ(ϢϺЄ)ΨᓕቘͽͣΡϫϚЄυϖςЄϠφ • → ϊЄφϊЄϖ΄ݐ஑/Ϡϸϖ/ϓφϕ;͚͜ϞαϤ϶αЀΨෆቘͽͣΡ • ϞαϤ϶αЀӤ΄ݱςЄϠφ΅φϓЄυ;޷Ό • →

    φϓЄυͽኞ౮ͫ΢͵ίЄϓΰϢήμϕ΅S3΁ᯈᗝͯΡͩ;΁ΞΠφϓЄυ樌ͽ昧൭ͯΡ • ϊЄφπЄϖ΄ݐ஑ض;ͭͼGitHubΨڥአݢᚆ • → ဳ఺ᅩ;ͭͼϣ϶ЀώΎ΄pushΨॶ䱛;ͯΡ஠ᥝ͘Π • → 匍䦒ᅩͽ΅όν΁๚䌏䖕 • CodePipeline͡ΟCloudFromation΄䋚ᤈ/Change Set֢౮/ๅෛ΀Ϳ͢ݢᚆ 33
  23. CodeBuild • buildspec.yml ΁चͻͣϠϸϖ/ϓφϕΨ䋚ෞͽͣΡϫϚЄυϖςЄϠ φ • → ϢδЄχ;͚༷͜ஷͽᇙਧ΄όαϬЀνͽձ఺΄πϫЀϖΨ䋚ᤈ ݢᚆ •

    → circle.yml ;य़֛ݶͮ • ςЄϠφϺЄϸ΁䌏䖕ͭͼ͚Ρ΄ͽAWSμϹϔЀτϰϸ΄咲ᤈ͢ӧᥝ • → CI/CD SaaS;吖΀Ρڥᅩ 34
  24. CI/CDϞαϤ϶αЀ - ق֛ 1. staging/productionϣ϶ЀώΎ΄ϫЄυΨॶ䱛΁CodePipeline䋚 ᤈ 2. GitHub͡ΟϊЄφπЄϖݐ஑ 3. CodeBuildͽϠϸϖ

    & ϓφϕ 4. CodePipeline͡ΟCloudFormation΄䋚ᤈ http://dev.classmethod.jp/cloud/aws/developing-cloudformation-ci-cd-pipeline-with-github-codebuild-codepipeline/ 35
  25. CI/CDϞαϤ϶Ѐ - Build & Test • CodeBuild͢೅୮ • ๋ڡ΁S3͡ΟϊЄφπЄϖΨݐ஑ •

    ϓφϕ΅ϳϘϐϕϓφϕ • ͩ΄䁰ݳ΄Ϡϸϖ;΅ aws cloudformation package Ψ೰ͯ • ኞ౮ͭ͵ίЄϓΰϢήμϕΨS3΁כਂ 37
  26. buildspec.yml΄ֺ version: 0.2 phases: install: commands: - | # AWS

    CLI pip install -U pip pip install -r requirements.txt # yarn curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb http://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list sudo apt-get update sudo apt-get install -y yarn yarn pre_build: commands: - | [ -d .sam ] || mkdir .sam aws cloudformation validate-template --template-body file://sam.yml yarn test build: commands: - | aws cloudformation package \ --template-file sam.yml \ --s3-bucket $S3_BUCKET \ --s3-prefix $S3_PREFIX \ --output-template-file .sam/packaged.yml artifacts: files: - .sam/* - params/* discard-paths: yes 38
  27. buildspec.yml΄ֺ - 厏ह΄ψϐϕίϐϤ • CodeBuild΁ϝЀϖϸͫ΢ͼ͚Ρ pip ΄ϝЄυϴЀݘ͚ • ௏Υ΂ε϶Є͢咲ኞͭ͢ͷ΀΄ͽϝЄυϴЀӤͨͼͥͩ͠; install:

    commands: - | # AWS CLI pip install -U pip pip install -r requirements.txt # yarn curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb http://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list sudo apt-get update sudo apt-get install -y yarn yarn 39
  28. buildspec.yml΄ֺ - ϓφϕ • AWS SAMϓЀϤϹЄϕ΄ϝϷϔЄτϴЀ;ϳϘϐϕϓφϕ • post_build ͽΚ͹ͼΘ͚͚ pre_build:

    commands: - | [ -d .sam ] || mkdir .sam aws cloudformation validate-template --template-body file://sam.yml yarn test 40
  29. buildspec.yml΄ֺ - Ϡϸϖ • ஍ͽϺЄϸϝϐμͫͱ͵͚䦒΄͵Η΁ S3_PREFIX Ψ෭՞΀Ϳ΁ͭ ͼͥ͠;Ξ͚ build: commands:

    - | aws cloudformation package \ --template-file sam.yml \ --s3-bucket $S3_BUCKET \ --s3-prefix $S3_PREFIX \ --output-template-file .sam/packaged.yml 41
  30. CI/CDϞαϤ϶Ѐ - Change Set΄֢౮ • CodePipeline͢೅୮ • S3͡ΟίЄϓΰϢήμϕΨݐ஑ • Template΁䄜䟵ͫ΢͵AWS

    SAMϓЀϤϹЄϕΨ೰ਧ • Template Configuration΁厏ह䶅΄Ϟ϶ϮЄόϢήαϸΨ೰ਧ { "Parameters": { "Env": "stg", "ReadCapacityUnits": "10", "WriteCapacityUnits": "10" } } 43
  31. Ք஍΄઀๕ • ςЄϝϹφίЄκϓμώϰͽϔϤϺαͯΡ厏ह΅嘦ᒈͫ΢ͼͣ͵ܦ᨝ • → AWS SAM/Serverless Framework΀Ϳޮ旽ϑЄϸ΄꧌䋚 • ͵ͶαЀϓνϹЄτϴЀϓφϕ

    or E2EϓφϕΨތΗ͵CI΄᮱ړͽᎣ憎͢੝΀͚ • → Localstackֵ͜;CIӤͽΘαЀφϕЄϸͭ΀͚;͚ͧ΀͚ • → ϳЄσԪֺΚϦφϕϤ϶μϓΰφ͢ΔͶ੝΀͚ • ϺЄθϸ樄咲厏हΨ꧌䋚ͫͱ͵͚ • → Localstack΅Քଙ咲ᤒͫ΢͵΄ͽΔͶΔͶ䱛ᚆӧ᪃΀ܦ᨝ • → ϯϐμ䨗ͥ΄τЀϖ͚ 47