トップ 差分 一覧 ソース 置換 検索 ヘルプ PDF RSS ログイン

localstackでapi gateway環境を作成する

参考

chatgptに聞けば教えてくれる

概要

localstackでapi gatewayの環境を構築する。
CloudFormation テンプレートを使う

各ファイル

docker-compose.yml

 version: "3.8"
 services:
   localstack:
     image: localstack/localstack
     container_name: localstack
     ports:
       - "4566:4566"
       - "4571:4571"
     environment:
       - SERVICES=lambda,apigateway,iam,logs,cloudformation
       - DEBUG=1
       - LAMBDA_EXECUTOR=docker
       - DEFAULT_REGION=ap-northeast-1
       - AWS_DEFAULT_REGION=ap-northeast-1
     volumes:
       - "/var/run/docker.sock:/var/run/docker.sock"
 
 
   aws-cli:
     image: amazon/aws-cli
     entrypoint: ""
     volumes:
       - ./:/work
       - aws:/root/.aws
 
     working_dir: /work
 
 volumes:
   aws:

template.yaml

 AWSTemplateFormatVersion: "2010-09-09"
 Resources:
   HelloLambda:
     Type: AWS::Lambda::Function
     Properties:
       FunctionName: HelloLambda
       Runtime: nodejs18.x
       Handler: index.handler
       Role: arn:aws:iam::000000000000:role/lambda-exec
       Code:
         ZipFile: |
           exports.handler = async () => {
             return {
               statusCode: 200,
               body: JSON.stringify({ message: "Hello from LocalStack CFN!" })
             };
           };
 
   ApiGatewayRestApi:
     Type: AWS::ApiGateway::RestApi
     Properties:
       Name: LocalApi
 
   ApiGatewayResourceHello:
     Type: AWS::ApiGateway::Resource
     Properties:
       RestApiId: !Ref ApiGatewayRestApi
       ParentId: !GetAtt ApiGatewayRestApi.RootResourceId
       PathPart: hello
 
   ApiGatewayMethodHelloGet:
     Type: AWS::ApiGateway::Method
     Properties:
       RestApiId: !Ref ApiGatewayRestApi
       ResourceId: !Ref ApiGatewayResourceHello
       HttpMethod: GET
       AuthorizationType: NONE
       Integration:
         Type: AWS_PROXY
         IntegrationHttpMethod: POST
         Uri:
           Fn::Sub: >-
             arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:000000000000:function:${HelloLambda}/invocations
 
   ApiGatewayDeployment:
     Type: AWS::ApiGateway::Deployment
     DependsOn: ApiGatewayMethodHelloGet
     Properties:
       RestApiId: !Ref ApiGatewayRestApi
       StageName: local

makefile

init:
	docker compose run --rm aws-cli aws configure
	# ap-northeast-1
up:
	docker compose up -d localstack

deploy:
	docker compose run --rm aws-cli \
	aws --endpoint-url=http://localstack:4566 cloudformation deploy \
	--template-file template.yaml \
	--stack-name local-api-stack \
	--capabilities CAPABILITY_NAMED_IAM

	API_ID=$$(docker compose run --rm aws-cli \
		aws --endpoint-url=http://localstack:4566 apigateway get-rest-apis \
			--query "items[0].id" --output text); \
	echo "API_ID = $$API_ID"; \
	curl "http://localhost:4566/restapis/$$API_ID/local/_user_request_/hello"

aws-cli:
	docker compose run --rm aws-cli /bin/bash


# aws --endpoint-url=http://localstack:4566 lambda create-function --function-name HelloLambda --runtime nodejs18.x --zip-file fileb://function.zip --handler index.handler --role arn:aws:iam::000000000000:role/lambda-exec
# aws --endpoint-url=http://localstack:4566 lambda create-function --function-name HelloLambda --runtime nodejs18.x --zip-file fileb://function.zip --handler index.handler --role arn:aws:iam::000000000000:role/lambda-exec

# aws --endpoint-url=http://localstack:4566 apigateway create-rest-api --name "LocalApi"

# aws --endpoint-url=http://localstack:4566 apigateway get-resources --rest-api-id $API_ID

# aws --endpoint-url=http://localstack:4566 apigateway create-resource --rest-api-id $API_ID --parent-id $ROOT_ID --path-part hello

# aws --endpoint-url=http://localstack:4566 apigateway put-method --rest-api-id $API_ID   --resource-id $HELLO_ID --http-method GET --authorization-type "NONE"

# aws --endpoint-url=http://localstack:4566 apigateway put-method --rest-api-id $API_ID --resource-id $HELLO_ID --http-method GET --authorization-type "NONE"

# aws --endpoint-url=http://localstack:4566 apigateway put-integration --rest-api-id $API_ID --resource-id $HELLO_ID --http-method GET --type AWS_PROXY --integration-http-method POST --uri arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:000000000000:function:HelloLambda/invocations

# aws --endpoint-url=http://localstack:4566 apigateway create-deployment --rest-api-id $API_ID --stage-name local

# curl http://localstack:4566/restapis/$API_ID/local/_user_request_/hello

実行

make init

で aws configure を実行。

  • AWS Access Key ID: hoge <- 何でも良い
  • AWS Secret Access Key: hoge <- 何でも良い
  • Default region name: ap-norteast-1
  • Default output format: json
make up

でlocalstack をバックグラウンド起動。

make deploy

でデプロイして、実行する

[カテゴリ: クラウド > AWS]

[通知用URL]



  • Hatenaブックマークに追加
  • livedoorクリップに追加
  • del.icio.usに追加
  • FC2ブックマークに追加

最終更新時間:2025年12月08日 23時23分20秒