\u3053\u3061\u3089<\/a>\u306e\u8a18\u4e8b\u3092\u5fa1\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n$ go mod init\ngo: creating new go.mod: module hoge\/fuga\/aws-sam-golang<\/code><\/pre>\n\u96db\u5f62\u306e\u5b9f\u884c\u30c6\u30b9\u30c8<\/h2>\n
sam local start-api<\/code> \u30b3\u30de\u30f3\u30c9\u3067\u3001\u30ed\u30fc\u30ab\u30eb\u3067\u3082Lambda\u95a2\u6570\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059(Docker\u4e0a\u3067\u52d5\u4f5c\u3057\u307e\u3059)\u3002<\/p>\nLambda\u95a2\u6570\u306e\u5b9f\u884c\u306b\u306f\u30d0\u30a4\u30ca\u30ea\u304c\u5fc5\u8981\u3067\u3059\u306e\u3067\u3001make<\/code>\u30b3\u30de\u30f3\u30c9\u3067\u30d3\u30eb\u30c9\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n$ make build\nGOOS=linux GOARCH=amd64 go build -o hello-world\/hello-world .\/hello-world\n\n$ sam local start-api\nMounting HelloWorldFunction at http:\/\/127.0.0.1:3000\/hello [GET]\nYou can now browse to the above endpoints to invoke your functions. You do not need to restart\/reload SAM CLI while working on your functions, changes will be reflected instantly\/automatically. You only need to restart SAM CLI if you update your AWS SAM template\n2020-01-19 20:38:36 * Running on http:\/\/127.0.0.1:3000\/ (Press CTRL+C to quit)\nInvoking hello-world (go1.x)<\/code><\/pre>\n\u3053\u306e\u72b6\u614b\u3067\u3001curl<\/code>\u30b3\u30de\u30f3\u30c9\u3067API\u3092\u53e9\u3044\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n$ curl http:\/\/localhost:3000\/hello\nHello, 118.***.***.**<\/code><\/pre>\n\u96db\u5f62\u306ehello-world\/main.go<\/code>\u306b\u5f93\u3044\u3001\u81ea\u5206\u306e\u30b0\u30ed\u30fc\u30d0\u30ebIP\u30a2\u30c9\u30ec\u30b9\u304c\u8fd4\u3063\u3066\u304d\u307e\u3057\u305f\u3002<\/p>\nSAM Local\u5074\u3067\u306f\u6b21\u306e\u3088\u3046\u306a\u30ed\u30b0\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n
Fetching lambci\/lambda:go1.x Docker container image......\nMounting \/Users\/daisakuhazui\/develop\/aws-sam-golang\/hello-world as \/var\/task:ro,delegated inside runtime container\nSTART RequestId: c0308535-1ed4-15d2-a58d-ff1ac09907ad Version: $LATEST\nEND RequestId: c0308535-1ed4-15d2-a58d-ff1ac09907ad\nREPORT RequestId: c0308535-1ed4-15d2-a58d-ff1ac09907ad Init Duration: 169.68 ms Duration: 891.58 ms Billed Duration: 900 ms Memory Size: 128 MB Max Memory Used: 27 MB\nNo Content-Type given. Defaulting to 'application\/json'.\n2020-01-19 20:38:58 127.0.0.1 - - [19\/Jan\/2020 20:38:58] "GET \/hello HTTP\/1.1" 200 -<\/code><\/pre>\nLambda\u95a2\u6570\u306e\u4fee\u6b63<\/h2>\n
\u96db\u5f62\u306e\u52d5\u4f5c\u78ba\u8a8d\u304c\u53d6\u308c\u305f\u306e\u3067\u3001\u6b21\u306fLambda\u95a2\u6570\u306b\u624b\u3092\u52a0\u3048\u307e\u3057\u3087\u3046\u3002<\/p>\n
\u5148\u307b\u3069\u78ba\u8a8d\u3057\u305f\u901a\u308a\u3001\u73fe\u72b6\u306e\u30b3\u30fc\u30c9\u306fhttp:\/\/localhost:3000\/hello<\/code>\u3078\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u306b\u5bfe\u3057IP\u30a2\u30c9\u30ec\u30b9\u3092\u8fd4\u3059\u5b9f\u88c5\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\u3053\u306e\u5b9f\u88c5\u3092http:\/\/localhost:3000\/hello\/{user}<\/code>\u306b\u5bfe\u3057\u3066Hello, {user}<\/code>\u3068\u8fd4\u3059\u3088\u3046\u3001\u3053\u308c\u304b\u3089\u4fee\u6b63\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n`main.go`\n\npackage main\n\n(\u4e2d\u7565)\n\nfunc handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {\n user, _ := request.PathParameters["user"]\n\n return events.APIGatewayProxyResponse{\n Body: fmt.Sprintf("Hello, %s", user),\n StatusCode: 200,\n }, nil\n}\n\nfunc main() {\n lambda.Start(handler)\n}<\/code><\/pre>\n\u95a2\u6570\u3092\u4fee\u6b63\u3057\u305f\u3089template.yaml<\/code>\u306b\u3082\u4fee\u6b63\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n`template.yaml`\nAWSTemplateFormatVersion: '2010-09-09'\nTransform: AWS::Serverless-2016-10-31\nDescription: >\n aws-sam-golang\n\n Sample SAM Template for aws-sam-golang\n\nGlobals:\n Function:\n Timeout: 5\n\nResources:\n HelloWorldFunction:\n Type: AWS::Serverless::Function\n Properties:\n CodeUri: hello-world\/\n Handler: hello-world\n Runtime: go1.x\n Tracing: Active\n Events:\n CatchAll:\n Type: Api\n Properties:\n Path: \/hello\/{user} # \u30d1\u30b9\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u30bb\u30c3\u30c8\n Method: GET\n Environment:\n Variables:\n PARAM1: VALUE\n\u30fb\n\u30fb\n\u30fb<\/code><\/pre>\nType: AWS::Serverless::Function<\/code>\u3067Lambda\u95a2\u6570\u3067\u3042\u308b\u3053\u3068\u3092\u8868\u3057\u3001Events<\/code>\u914d\u4e0b\u306eCatchAll<\/code>\u3067API\u306e\u5b9a\u7fa9\u3092\u884c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\u4eca\u56de\u306f\u30d1\u30b9\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u53d7\u3051\u53d6\u308c\u308b\u3088\u3046\u306bPath<\/code>\u306e\u5024\u3092\u4fee\u6b63\u3057\u307e\u3057\u305f\u3002<\/p>\n\u52d5\u4f5c\u78ba\u8a8d<\/h2>\n
\u95a2\u6570\u3068\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u4fee\u6b63\u304c\u7d42\u308f\u3063\u305f\u306e\u3067\u30ed\u30fc\u30ab\u30eb\u3067\u52d5\u4f5c\u78ba\u8a8d\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n
$ make build\nGOOS=linux GOARCH=amd64 go build -o hello-world\/hello-world .\/hello-world\n\n$ sam local start-api\nMounting HelloWorldFunction at http:\/\/127.0.0.1:3000\/hello\/{user} [GET]\nYou can now browse to the above endpoints to invoke your functions. You do not need to restart\/reload SAM CLI while working on your functions, changes will be reflected instantly\/automatically. You only need to restart SAM CLI if you update your AWS SAM template\n2020-01-19 20:47:07 * Running on http:\/\/127.0.0.1:3000\/ (Press CTRL+C to quit)\nInvoking hello-world (go1.x)<\/code><\/pre>\n\u4eca\u5ea6\u306f\u30d1\u30b9\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u4e0e\u3048\u3066curl<\/code>\u30b3\u30de\u30f3\u30c9\u3092\u53e9\u3044\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n$ curl http:\/\/localhost:3000\/hello\/Daisaku\nHello, Daisaku<\/code><\/pre>\nOK\u3067\u3059\u3002\u60f3\u5b9a\u901a\u308a\u306e\u52d5\u304d\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n
AWS\u4e0a\u306b\u30c7\u30d7\u30ed\u30a4<\/h2>\n
\u30ed\u30fc\u30ab\u30eb\u3067\u52d5\u4f5c\u78ba\u8a8d\u3092\u7d42\u3048\u305f\u306e\u3067\u3001\u5b9f\u969b\u306bAWS\u4e0a\u306b\u30c7\u30d7\u30ed\u30a4\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n
$ sam validate --profile iamSAM\n\/Users\/daisakuhazui\/develop\/aws-sam-golang\/template.yaml is a valid SAM Template\n\n$ sam package \n --template-file template.yaml \n --s3-bucket <YOUR BUCKET NAME> \n --output-template-file packaged.yaml \n --profile iamSAM\n\nUploading to a1342a2f62dce02aba5dfa11e11a6d1d 5023955 \/ 5023955.0 (100.00%)\n\nSuccessfully packaged artifacts and wrote output template to file packaged.yaml.\nExecute the following command to deploy the packaged template\nsam deploy --template-file \/Users\/daisakuhazui\/develop\/aws-sam-golang\/packaged.yaml --stack-name <YOUR STACK NAME>\n\n$ sam deploy \n --profile iamSAM \n --region ap-northeast-1 \n --template-file \/Users\/daisakuhazui\/develop\/aws-sam-golang\/packaged.yaml \n --stack-name aws-sam-golang \n --capabilities CAPABILITY_IAM\n\n Deploying with following values\n ===============================\n Stack name : aws-sam-golang\n Region : ap-northeast-1\n Confirm changeset : False\n Deployment s3 bucket : None\n Capabilities : ["CAPABILITY_IAM"]\n Parameter overrides : {}\n\nInitiating deployment\n=====================\n\nWaiting for changeset to be created..\n\n(\u4e2d\u7565)\n\nStack aws-sam-golang outputs:\n---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\nOutputKey-Description OutputValue\n---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\nHelloWorldFunctionIamRole - Implicit IAM Role created for Hello World function arn:aws:iam::************:role\/aws-sam-golang-HelloWorldFunctionRole-*************\nHelloWorldAPI - API Gateway endpoint URL for Prod environment for First Function https:\/\/**********.execute-api.ap-northeast-1.amazonaws.com\/Prod\/hello\/\nHelloWorldFunction - First Lambda Function ARN arn:aws:lambda:ap-northeast-1:************:function:aws-sam-golang-\n HelloWorldFunction-*************\n---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\nSuccessfully created\/updated stack - aws-sam-golang in ap-northeast-1<\/code><\/pre>\n\u3053\u308c\u3067AWS\u4e0a\u3078\u306e\u30c7\u30d7\u30ed\u30a4\u304c\u5b8c\u4e86\u3057\u307e\u3057\u305f\u3002<\/p>\n
\u6700\u5f8c\u306bAPI Gateway\u306e\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002(API Gateway endpoint URL for Prod environment for First Function<\/code>\u306e\u53f3\u306b\u3042\u308bURL)<\/p>\n$ curl https:\/\/**********.execute-api.ap-northeast-1.amazonaws.com\/Prod\/hello\/Daisaku\nHello, Daisaku<\/code><\/pre>\nAPI Gateway\u7d4c\u7531\u3067\u3082\u671f\u5f85\u901a\u308a\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u8fd4\u3063\u3066\u304d\u307e\u3057\u305f\u3002<\/p>\n
\u307e\u3068\u3081<\/h2>\n
AWS SAM\u3068Go\u8a00\u8a9e\u3092\u4f7f\u3063\u3066\u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u306a\u30b5\u30fc\u30d0\u30fc\u30ec\u30b9\u69cb\u6210\u3092\u69cb\u7bc9\u3057\u307e\u3057\u305f\u3002<\/p>\n
\u6628\u5e74\u306e\u30a2\u30c3\u30d7\u30c7\u30fc\u30c8\u3067AWS SAM\u306e\u30c7\u30d7\u30ed\u30a4\u65b9\u6cd5\u304c\u975e\u5e38\u306b\u7c21\u5358\u306b\u306a\u308a\u3001\u3068\u3066\u3082\u4f7f\u3044\u3084\u3059\u304f\u306a\u3063\u305f\u3093\u3058\u3083\u306a\u3044\u304b\u306a\u3068\u601d\u3044\u307e\u3059\u3002(sam package<\/code>\u3068sam deploy<\/code>\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u306f\u8907\u96d1\u3067\u3059\u304c...)<\/p>\n\u53c2\u8003\u306b\u306a\u308c\u3070\u5e78\u3044\u3067\u3059\u3002<\/p>\n
\u8fd1\u5e74\u3001MMM\u306fAWS Lambda\u306b\u529b\u3092\u5165\u308c\u3066\u3044\u307e\u3059\u3002\u305c\u3072\u4ee5\u4e0b\u306e\u30da\u30fc\u30b8\u3082\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n
\u30fb\u30b5\u30fc\u30d0\u30fc\u30ec\u30b9\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\uff08AWS Lambda\uff09<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\u4eca\u56de\u306fAWS SAM\u3092\u4f7f\u7528\u3057\u3066\u30b5\u30fc\u30d0\u30fc\u30ec\u30b9\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u69cb\u7bc9\u3057\u3066\u3044\u304d\u307e\u3059\u3002 API Gateway\u3092\u30c8\u30ea\u30ac\u30fc\u3068\u3057\u3066Lambda\u95a2\u6570\u304c\u767a\u706b\u3059\u308b\u3068\u3044\u3046\u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u306a\u5185\u5bb9\u3067\u3059\u3002\u305d\u306e\u305f\u3081\u30b5\u30fc\u30d0\u30fc\u30ec\u30b9\u958b\u767a\u306e\u30a4\u30e1\u30fc\u30b8\u3092\u63b4\u307f\u3084\u3059\u3044\u306e\u3067\u306f\u306a\u3044\u304b\u3068\u601d\u3044\u307e\u3059\u3002 AWS SAM\u3068\u306f AWS SAM(Serverless Application Model)\u3068\u306f\u3001AWS\u4e0a\u306b\u30b5\u30fc\u30d0\u30fc\u30ec\u30b9\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u69cb\u7bc9\u3059\u308b\u3053\u3068 […]<\/p>\n","protected":false},"author":9,"featured_media":826,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13,21],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/603"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=603"}],"version-history":[{"count":0,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/603\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media\/826"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}