aws cloudformationは何となく分かったので、SAM(Serverless Application Model)でHello,worldしてみた。意外と大変!
参考URL
https://qiita.com/y4u0t2a1r0/items/2d27eed7afd4e4ffbab5
概要:
1, ローカルのSAMで、cloudformationのhello,worldテンプレートを作成
2, スタックその1(aws-sam-cli-managed-default)が生成され、S3にcloudformationのテンプレートがアップロードされる
3, S3のテンプレートを元に、スタックその2(sam-app)が生成され、Hello,worldが出来る。
Windows10に、AWS SAM CLI のインストール。どっかにDocker必要って書いてあったような気がするけど、普通にcliだけで動作した。
https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/serverless-sam-cli-install-windows.html
1, 対話式にSAMプロジェクト生成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
sam --version SAM CLI, version 0.53.0 適当なフォルダを作って # samプロジェクト生成 sam init # 1 - AWS Quick Start Templatesを選択 Which template source would you like to use? 1 - AWS Quick Start Templates 2 - Custom Template Location # 1 - nodejs12.xを選択 Which runtime would you like to use? 1 - nodejs12.x 2 - python3.8 3 - ruby2.7 4 - go1.x 5 - java11 6 - dotnetcore3.1 7 - nodejs10.x 8 - python3.7 9 - python3.6 10 - python2.7 11 - ruby2.5 12 - java8 13 - dotnetcore2.1 #適当な名前を入力 Project name [sam-app]: sample Cloning app templates from https://github.com/awslabs/aws-sam-cli-app-templates.git # 1 - Hello World Exampleを選択 AWS quick start application templates: 1 - Hello World Example 2 - Step Functions Sample App (Stock Trader) 3 - Quick Start: From Scratch 4 - Quick Start: Scheduled Events 5 - Quick Start: S3 6 - Quick Start: SNS 7 - Quick Start: SQS 8 - Quick Start: Web Backend # サンプルSAMが出来た。 ----------------------- Generating application: ----------------------- Name: sample Runtime: nodejs12.x Dependency Manager: npm Application Template: hello-world Output Directory: . Next steps can be found in the README file at ./sample/README.md |
2, ビルド&デプロイ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# フォルダ内に入ってビルド cd sample sam build # ローカルからサーバへデプロイ sam deploy --guided Configuring SAM deploy ====================== Looking for samconfig.toml : Not found Setting default arguments for 'sam deploy' ========================================= Stack Name [sam-app]: AWS Region [us-east-1]: ap-northeast-1 #Shows you resources changes to be deployed and require a 'Y' to initiate deploy Confirm changes before deploy [y/N]: #SAM needs permission to be able to create roles to connect to the resources in your template Allow SAM CLI IAM role creation [Y/n]: HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: Error: Security Constraints Not Satisfied! |
3, 上手く行かなかったので、オプションを変えて再トライ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
sam deploy -g Configuring SAM deploy ====================== Looking for samconfig.toml : Not found Setting default arguments for 'sam deploy' ========================================= Stack Name [sam-app]: AWS Region [us-east-1]: ap-northeast-1 #とりあえず全部YESで乗り切る! #Shows you resources changes to be deployed and require a 'Y' to initiate deploy Confirm changes before deploy [y/N]: yes #SAM needs permission to be able to create roles to connect to the resources in your template Allow SAM CLI IAM role creation [Y/n]: yes HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: yes Save arguments to samconfig.toml [Y/n]: yes Looking for resources needed for deployment: Not found. Creating the required resources... Successfully created! Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-mfkboxvzucqa A different default S3 bucket can be set in samconfig.toml Saved arguments to config file Running 'sam deploy' for future deployments will use the parameters saved above. The above parameters can be changed by modifying samconfig.toml Learn more about samconfig.toml syntax at https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html Deploying with following values =============================== Stack name : sam-app Region : ap-northeast-1 Confirm changeset : True Deployment s3 bucket : aws-sam-cli-managed-default-samclisourcebucket-mfkboxvzucqa Capabilities : ["CAPABILITY_IAM"] Parameter overrides : {} Initiating deployment ===================== Uploading to sam-app/501efac4ca06876e22690cc5d59905e3 127562 / 127562.0 (100.00%) HelloWorldFunction may not have authorization defined. Uploading to sam-app/d254a293071fa2cbccb33d9a3da2aee1.template 1127 / 1127.0 (100.00%) Waiting for changeset to be created.. CloudFormation stack changeset --------------------------------------------------------------------------------------------------------------------- Operation LogicalResourceId ResourceType --------------------------------------------------------------------------------------------------------------------- + Add HelloWorldFunctionHelloWorldPermissio AWS::Lambda::Permission nProd + Add HelloWorldFunctionRole AWS::IAM::Role + Add HelloWorldFunction AWS::Lambda::Function + Add ServerlessRestApiDeployment47fc2d5f9d AWS::ApiGateway::Deployment + Add ServerlessRestApiProdStage AWS::ApiGateway::Stage + Add ServerlessRestApi AWS::ApiGateway::RestApi --------------------------------------------------------------------------------------------------------------------- Changeset created successfully. arn:aws:cloudformation:ap-northeast-1:xxxxxxxxxxxx:changeSet/samcli-deploy1595219991/d523abac-06f9-41c5-aa3c-aea591b87876 Previewing CloudFormation changeset before deployment ====================================================== #とりあえず全部YESで乗り切る! Deploy this changeset? [y/N]: y 2020-07-20 13:40:50 - Waiting for stack create/update to complete CloudFormation events from changeset --------------------------------------------------------------------------------------------------------------------- ResourceStatus ResourceType LogicalResourceId ResourceStatusReason --------------------------------------------------------------------------------------------------------------------- CREATE_IN_PROGRESS AWS::IAM::Role HelloWorldFunctionRole Resource creation Initiated CREATE_IN_PROGRESS AWS::IAM::Role HelloWorldFunctionRole - CREATE_COMPLETE AWS::IAM::Role HelloWorldFunctionRole - CREATE_IN_PROGRESS AWS::Lambda::Function HelloWorldFunction - CREATE_IN_PROGRESS AWS::Lambda::Function HelloWorldFunction Resource creation Initiated CREATE_COMPLETE AWS::Lambda::Function HelloWorldFunction - CREATE_IN_PROGRESS AWS::ApiGateway::RestApi ServerlessRestApi - CREATE_IN_PROGRESS AWS::ApiGateway::RestApi ServerlessRestApi Resource creation Initiated CREATE_COMPLETE AWS::ApiGateway::RestApi ServerlessRestApi - CREATE_IN_PROGRESS AWS::ApiGateway::Deployment ServerlessRestApiDeployment - 47fc2d5f9d CREATE_IN_PROGRESS AWS::ApiGateway::Deployment ServerlessRestApiDeployment Resource creation Initiated 47fc2d5f9d CREATE_IN_PROGRESS AWS::Lambda::Permission HelloWorldFunctionHelloWorl Resource creation Initiated dPermissionProd CREATE_IN_PROGRESS AWS::Lambda::Permission HelloWorldFunctionHelloWorl - dPermissionProd CREATE_COMPLETE AWS::ApiGateway::Deployment ServerlessRestApiDeployment - 47fc2d5f9d CREATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdStage - CREATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdStage Resource creation Initiated CREATE_COMPLETE AWS::ApiGateway::Stage ServerlessRestApiProdStage - CREATE_COMPLETE AWS::Lambda::Permission HelloWorldFunctionHelloWorl - dPermissionProd CREATE_COMPLETE AWS::CloudFormation::Stack sam-app - --------------------------------------------------------------------------------------------------------------------- CloudFormation outputs from deployed stack --------------------------------------------------------------------------------------------------------------------- Outputs --------------------------------------------------------------------------------------------------------------------- Key HelloWorldFunctionIamRole Description Implicit IAM Role created for Hello World function Value arn:aws:iam::xxxxxxxxxxxx:role/sam-app-HelloWorldFunctionRole-WH7Y73B5DUSD Key HelloWorldApi Description API Gateway endpoint URL for Prod stage for Hello World function Value https://yi42p72683.execute-api.ap-northeast-1.amazonaws.com/Prod/hello/ Key HelloWorldFunction Description Hello World Lambda Function ARN Value arn:aws:lambda:ap-northeast-1:xxxxxxxxxxxx:function:sam-app-HelloWorldFunction-345LLS3FMX0H --------------------------------------------------------------------------------------------------------------------- Successfully created/updated stack - sam-app in ap-northeast-1 |
4, hello,wordしたいだけなのに、えらいリソースが作られた…。
これを見るに、API Gateway経由でlambdaを読み込んでhello,worldするんだろうね(Roleはlambda実行権限)
AWS::Lambda::Function
AWS::Lambda::Permission
AWS::IAM::Role
AWS::ApiGateway::RestApi
AWS::ApiGateway::Deployment
AWS::ApiGateway::Stage
api gatewayのダッシュボードにあるURLの末尾にhelloメソッドを付けてアクセスすると、{“message”:”hello world”}と返ってくる(コンソールの最後にもURLは表示される)
https://yi42p72683.execute-api.ap-northeast-1.amazonaws.com/Prod/hello
デプロイの権限とか特に入力しなかったんだけど、aws-cliの奴を使ったのかな?
スタックの削除は、SAMから出来ない?
コンソール画面から削除するか、コマンドラインのみ?
aws cloudformation delete-stack –stack-name sam-app
S3が邪魔でスタックが削除出来ないというので、手動で削除。これで正しいのだろうか…。