{"id":1626,"date":"2021-06-25T19:50:50","date_gmt":"2021-06-25T10:50:50","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/?p=1626"},"modified":"2021-06-25T19:50:50","modified_gmt":"2021-06-25T10:50:50","slug":"serverless-framework-stack","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2021\/06\/25\/serverless-framework-stack\/","title":{"rendered":"Serverless Framework \u3067\u30b9\u30bf\u30c3\u30af\u3092\u5206\u5272\u3057\u3066\u30c7\u30d7\u30ed\u30a4\u3059\u308b"},"content":{"rendered":"
\u3053\u3093\u306b\u3061\u306f\u3001\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u7267\u7530\u3067\u3059\u3002\u6700\u8fd1\u300cAWS Certified Developer \u2013\u00a0Associate\u300d\u306b\u5408\u683c\u3057\u307e\u3057\u305f\u3002\u4eca\u56de\u306fServerless Framework\u3092\u4f7f\u7528\u3057\u3066\u69d8\u3005\u306a\u30ea\u30bd\u30fc\u30b9\u3092\u30c7\u30d7\u30ed\u30a4\u3059\u308b\u3068\u304d\u306b\u3001\u30b9\u30bf\u30c3\u30af\u3092\u5206\u5272\u3059\u308b\u3053\u3068\u306e\u30e1\u30ea\u30c3\u30c8\u3084\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n
MMM\u3067\u306f\u3001\u30b5\u30fc\u30d0\u30fc\u30ec\u30b9\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3059\u308b\u969b\u306b\u3001Serverless Framework\u3092\u7528\u3044\u308b\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002Serverless Framework\u306f\u30c7\u30d7\u30ed\u30a4\u6642\u306b\u88cf\u5074\u3067\u306fCloud Formation\u304c\u52d5\u3044\u3066\u304a\u308a\u3001serverless.yml\u306bCloud Formation\u306e\u8a18\u8ff0\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u3001API Gateway\u30fbLambda\u306e\u4ed6\u306b\u3082\u3001\u69d8\u3005\u306a\u30ea\u30bd\u30fc\u30b9\u3092\u30c7\u30d7\u30ed\u30a4\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n
\u4eca\u56de\u306f\u4ee5\u4e0b\u306e\u30ea\u30bd\u30fc\u30b9\u3092\u30c7\u30d7\u30ed\u30a4\u3059\u308b\u3053\u3068\u3092\u8003\u3048\u3066\u307f\u307e\u3059\u3002<\/p>\n
\u30fbVPC\u306a\u3069
\n\u30fbDB
\n\u30fbAPI Gateway \/ Lambda(VPC\u5185)<\/p>\n
Serverless Framework\u3067\u306f\u30011\u3064\u306eserverless.yml\u30d5\u30a1\u30a4\u30eb\u304cCloud Formation\u306e1\u3064\u306e\u30b9\u30bf\u30c3\u30af\u3068\u306a\u308a\u307e\u3059\u304c\u3001\u3053\u308c\u3089\u306e\u5168\u3066\u306e\u30ea\u30bd\u30fc\u30b9\u30921\u3064\u306e\u30b9\u30bf\u30c3\u30af\u3067\u30c7\u30d7\u30ed\u30a4\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u70b9\u304c\u6c17\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n
\u3053\u306e\u3088\u3046\u306a\u554f\u984c\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u3001\u30b9\u30bf\u30c3\u30af\u3092\u5206\u5272\u3059\u308b\u3053\u3068\u3092\u8003\u3048\u307e\u3059\u3002\u30b9\u30bf\u30c3\u30af\u3092\u5206\u5272\u3059\u308b\u5834\u5408\u306f\u3001\u30ea\u30bd\u30fc\u30b9\u306e\u307e\u3068\u307e\u308a\u3054\u3068\u3084\u3001\u30e9\u30a4\u30d5\u30b5\u30a4\u30af\u30eb\uff08\u3069\u308c\u304f\u3089\u3044\u306e\u983b\u5ea6\u3067\u66f4\u65b0\u3055\u308c\u308b\u304b\u306a\u3069\uff09\u3092\u5143\u306b\u5206\u3051\u308b\u3068\u3044\u3044\u3067\u3057\u3087\u3046\u3002\u4eca\u56de\u306e\u5834\u5408\u3001VPC\u95a2\u9023\u3084DB\u306f\u4e00\u5ea6\u30c7\u30d7\u30ed\u30a4\u3057\u305f\u3089\u305d\u3046\u305d\u3046\u5909\u308f\u308b\u3053\u3068\u306f\u306a\u3044\u306e\u306b\u5bfe\u3057\u3001API\u30fbLambda\u306f\u983b\u7e41\u306b\u5909\u66f4\u30fb\u8ffd\u52a0\u304c\u3055\u308c\u308b\u3082\u306e\u3067\u3059\u3002\u306a\u306e\u3067\u3001\u3053\u306e2\u3064\u306b\u5206\u3051\u3066\u30b9\u30bf\u30c3\u30af\u3092\u4f5c\u6210\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n
\u307e\u305f\u3001Cloud Formation\u306b\u306f1\u30b9\u30bf\u30c3\u30af\u3042\u305f\u308a\u306e\u30ea\u30bd\u30fc\u30b9\u306f200\u500b\u307e\u3067\u3068\u3044\u3046\u5236\u9650\u304c\u3042\u308a\u307e\u3059\u3002\u30b9\u30bf\u30c3\u30af\u3092\u5206\u5272\u3059\u308b\u3053\u3068\u3067\u3001\u3053\u306e\u5236\u9650\u306b\u9054\u3059\u308b\u3053\u3068\u306e\u9632\u6b62\u306b\u3082\u306a\u308a\u307e\u3059\u3002<\/p>\n
\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u69cb\u6210\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002\uff08\u5177\u4f53\u7684\u306a\u95a2\u6570\u7b49\u306e\u30d5\u30a1\u30a4\u30eb\u306f\u7701\u7565\u3057\u3066\u3044\u307e\u3059\uff09
\ndb.yml\u30fbvpc.yml\u306f\u3001\u30ea\u30bd\u30fc\u30b9\u3092\u5225\u306e\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u66f8\u3044\u3066\u3044\u307e\u3059\u3002\u9577\u304f\u306a\u308b\u90e8\u5206\u306f\u5206\u5272\u3059\u308b\u3053\u3068\u3067serverless.yml\u304c\u30b9\u30c3\u30ad\u30ea\u3057\u3066\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u3082\u3057\u3084\u3059\u304f\u306a\u308a\u307e\u3059\u3002\u307e\u305f\u3001\uff08\u4eca\u56de\u306e\u90e8\u5206\u306b\u306f\u95a2\u4fc2\u306a\u3044\u3067\u3059\u304c\uff09\u8a00\u8a9e\u306fGo\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n
.\n\u251c\u2500\u2500 serverless\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 api\n\u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u2514\u2500\u2500 serverless.yml\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 vpc-db\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 db.yml\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 serverless.yml\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 vpc.yml\n\u2514\u2500\u2500 .env<\/code><\/pre>\n\u30b3\u30fc\u30c9\u4f8b<\/h2>\n\n- vpc-db\/serverless.yml<\/li>\n<\/ul>\n
service: sample-vpc\n\ncustom:\n dotenv:\n path: ..\/..\/\n stage: ${env:STAGE}\n name: sample-vpc-${env:STAGE}\n\nplugins:\n - serverless-dotenv-plugin\n\nprovider:\n name: aws\n runtime: go1.x\n stage: ${self:custom.stage}\n region: ap-northeast-1\n stackName: ${self:custom.name}-stack\n\nresources:\n # \u5225\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u30ea\u30bd\u30fc\u30b9\u3092\u53c2\u7167\n - ${file(.\/vpc.yml)}\n - ${file(.\/db.yml)}\n\n # \u5225\u30b9\u30bf\u30c3\u30af\u3067\u5fc5\u8981\u306a\u5024\u3092\u51fa\u529b\n - Outputs:\n LambdaSecurityGroup:\n Value: !Ref LambdaSecurityGroup\n Export:\n Name: LambdaSecurityGroup-${self:custom.stage}\n PrivateSubnetA:\n Value: !Ref PrivateSubnetA\n Export:\n Name: PrivateSubnetA-${self:custom.stage}\n PrivateSubnetC:\n Value: !Ref PrivateSubnetC\n Export:\n Name: PrivateSubnetC-${self:custom.stage}<\/code><\/pre>\n\n- vpc-db\/vpc.yml<\/li>\n<\/ul>\n
Resources:\n VPC:\n Type: AWS::EC2::VPC\n Properties:\n CidrBlock: 10.0.0.0\/16\n Tags:\n - Key: Name\n Value: sample-vpc\n PrivateSubnetA:\n Type: AWS::EC2::Subnet\n Properties:\n VpcId: !Ref VPC\n CidrBlock: 10.0.1.0\/24\n AvailabilityZone: ap-northeast-1a\n Tags:\n - Key: Name\n Value: sample-subnet-a\n PrivateSubnetC:\n Type: AWS::EC2::Subnet\n Properties:\n VpcId: !Ref VPC\n CidrBlock: 10.0.2.0\/24\n AvailabilityZone: ap-northeast-1c\n Tags:\n - Key: Name\n Value: sample-subnet-c\n # \u4ed6\u306e\u30ea\u30bd\u30fc\u30b9\u306f\u7701\u7565<\/code><\/pre>\n\n- api\/serverless.yml<\/li>\n<\/ul>\n
service: sample-api\n\ncustom:\n dotenv:\n path: ..\/..\/\n stage: ${env:STAGE}\n name: sample-api-${env:STAGE}\n\nplugins:\n - serverless-dotenv-plugin\n\nprovider:\n name: aws\n runtime: go1.x\n stage: ${self:custom.stage}\n region: ap-northeast-1\n apiName: ${self:custom.name}-api\n stackName: ${self:custom.name}-cfs-api\n vpc:\n # \u5225\u30b9\u30bf\u30c3\u30af\u304b\u3089Export\u3055\u308c\u305f\u5024\u3092\u8aad\u307f\u8fbc\u3080\n securityGroupIds:\n - !ImportValue LambdaSecurityGroup-${self:custom.stage}\n subnetIds:\n - !ImportValue PrivateSubnetA-${self:custom.stage}\n - !ImportValue PrivateSubnetC-${self:custom.stage}\n iam:\n role:\n # \uff08\u7701\u7565\uff09\n\nfunctions:\n GetSamples:\n events:\n - http:\n method: get\n path: \/samples\n handler: handlers\/api\/main\n# \u306a\u3069\u306e\u95a2\u6570\uff08\u5b9f\u969b\u306f\u5225\u30d5\u30a1\u30a4\u30eb\u3067\u66f8\u3044\u3066\u3044\u307e\u3059\uff09\n<\/code><\/pre>\n\u4f7f\u7528\u3057\u3066\u3044\u308b\u30d7\u30e9\u30b0\u30a4\u30f3\u306b\u95a2\u3057\u3066\u3067\u3059\u304c\u3001serverless-dotenv-plugin\u3092\u4f7f\u3063\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306f\u3001.env\u30d5\u30a1\u30a4\u30eb\u306b\u66f8\u3044\u305f\u74b0\u5883\u5909\u6570\u3092serverless.yml\u306b\u8aad\u307f\u8fbc\u3080\u305f\u3081\u306e\u3082\u306e\u3067\u3059\u3002${env:STAGE}<\/code>\u306e\u3088\u3046\u306b\u8aad\u307f\u8fbc\u3080\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u6ce8\u610f\u70b9\u3068\u3057\u3066\u3001.env\u3068serverless.yml\u306e\u5834\u6240\u304c\u7570\u306a\u308b\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u90e8\u5206\u306e\u3088\u3046\u306bserverless.yml\u304b\u3089\u898b\u305f.env\u306e\u30d1\u30b9\u3092\u6307\u5b9a\u3057\u3066\u3042\u3052\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\ncustom:\n dotenv:\n path: ..\/..\/<\/code><\/pre>\n\u5225\u30b9\u30bf\u30c3\u30af\u306e\u5024\u3092\u53c2\u7167\u3059\u308b\u65b9\u6cd5<\/h3>\n
\u4eca\u56de\u306e\u5834\u5408\u3001Lambda\u304cVPC\u5185\u306b\u3042\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3001VPC\u306b\u95a2\u3059\u308b\u60c5\u5831\u3092vpc-db\u30b9\u30bf\u30c3\u30af\u304b\u3089api\u30b9\u30bf\u30c3\u30af\u306b\u6e21\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002Cloud Formation\u306eOutputs\u3068\u3044\u3046\u6a5f\u80fd\u3092\u4f7f\u7528\u3057\u3066\u3053\u308c\u3092\u5b9f\u73fe\u3057\u307e\u3059\u3002<\/p>\n
\u307e\u305a\u3001vpc-db\u5074\u306eserverless.yml\u3067\u3001\u4ee5\u4e0b\u306e\u90e8\u5206\u3067\u5fc5\u8981\u306a\u9805\u76ee\u3092export\u3057\u307e\u3059\u3002<\/p>\n
- Outputs:\n LambdaSecurityGroup:\n Value: !Ref LambdaSecurityGroup\n Export:\n Name: LambdaSecurityGroup-${self:custom.stage}\n PrivateSubnetA:\n Value: !Ref PrivateSubnetA\n Export:\n Name: PrivateSubnetA-${self:custom.stage}\n PrivateSubnetC:\n Value: !Ref PrivateSubnetC\n Export:\n Name: PrivateSubnetC-${self:custom.stage}<\/code><\/pre>\n\u305d\u3057\u3066\u3001api\u5074\u306eserverless.yml\u3067\u306f\u3001\u4ee5\u4e0b\u306e\u90e8\u5206\u3067export\u3057\u305f\u5024\u3092import\u3057\u3066\u3044\u307e\u3059\u3002\u6ce8\u610f\u70b9\u3068\u3057\u3066\u3001serverless.yml\u306ecustom:\u306e\u90e8\u5206\u3067\u306fImportValue\u95a2\u6570\u306f\u4f7f\u3048\u307e\u305b\u3093\u3002<\/p>\n
vpc:\n # \u5225\u30b9\u30bf\u30c3\u30af\u304b\u3089Export\u3055\u308c\u305f\u5024\u3092\u8aad\u307f\u8fbc\u3080\n securityGroupIds:\n - !ImportValue LambdaSecurityGroup-${self:custom.stage}\n subnetIds:\n - !ImportValue PrivateSubnetA-${self:custom.stage}\n - !ImportValue PrivateSubnetC-${self:custom.stage}<\/code><\/pre>\n