sam deploy時にエラーではまった話

概要

先の記事でコンテナLambdaを実行できるようになりました。

AWS SAMでコンテナLambdaを作成する

その後、app.pyを修正して再度sam deployしたときに以下のエラーが表示され、
原因が分かるのに時間がかかったので、備忘録として記録しておきます。

1
Error: Only one of the following can be provided: '--image-repositories', '--image-repository', or '--resolve-image-repos'. Do you have multiple specified in the command or in a configuration file?

エラーの原因

エラーの原因は samconfig.toml に、デフォルトで余計な設定が書かれており、
–guidedを使ってデプロイした時に追記されるパラメータと競合していたことが原因でした。

samconfig.toml

デフォルトの samconfig.toml

デフォルトでは samconfig.toml は以下の記載になっています。

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
# More information about the configuration file can be found here:
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
version = 0.1

[default]
[default.global]
[default.global.parameters]
stack_name = "sam-app"

[default.build]
[default.build.parameters]
parallel = true

[default.validate]
[default.validate.parameters]
lint = true

[default.deploy]
[default.deploy.parameters]
capabilities = "CAPABILITY_IAM"
confirm_changeset = true
resolve_s3 = true
resolve_image_repos = true

[default.package]
[default.package.parameters]
resolve_s3 = true

[default.sync]
[default.sync.parameters]
watch = true

[default.local_start_api]
[default.local_start_api.parameters]
warm_containers = "EAGER"

[default.local_start_lambda]
[default.local_start_lambda.parameters]
warm_containers = "EAGER"

注目すべきは、以下の記載箇所です

1
2
3
4
5
6
[default.deploy]
[default.deploy.parameters]
capabilities = "CAPABILITY_IAM"
confirm_changeset = true
resolve_s3 = true
resolve_image_repos = true

sam deploy –guided コマンドでデプロイ

以下のように、sam deploy –guidedコマンドでデプロイします。

途中の選択肢はハンズオンや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
$ sam deploy --guided

Configuring SAM deploy
======================

Looking for config file [samconfig.toml] : Found
Reading default arguments : Success

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]:
#Preserves the state of previously provisioned resources when an operation fails
Disable rollback [y/N]:
HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
Save arguments to configuration file [Y/n]:
SAM configuration file [samconfig.toml]:
SAM configuration environment [default]:

Looking for resources needed for deployment:

Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-1234567890
A different default S3 bucket can be set in samconfig.toml
Image repositories: Not found.
#Managed repositories will be deleted when their functions are removed from the template and deployed
Create managed ECR repositories for all functions? [Y/n]:

Parameter "stack_name=sam-app" in [default.deploy.parameters] is defined as a global parameter [default.global.parameters].
This parameter will be only saved under [default.global.parameters] in /home/user/Project/sam-app/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

デプロイ後の samconfig.toml

デプロイ後には samconfig.toml は以下の記載になっていました。

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
# More information about the configuration file can be found here:
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
version = 0.1

[default]
[default.global]
[default.global.parameters]
stack_name = "sam-app"

[default.build]
[default.build.parameters]
parallel = true

[default.validate]
[default.validate.parameters]
lint = true

[default.deploy]
[default.deploy.parameters]
capabilities = "CAPABILITY_IAM"
confirm_changeset = true
resolve_s3 = true
resolve_image_repos = true
s3_bucket = "aws-sam-cli-managed-default-samclisourcebucket-1234567890"
s3_prefix = "sam-app"
region = "ap-northeast-1"
image_repositories = ["HelloWorldFunction=123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/samapp2b0162595/helloworldfunction19d43fc4repo"]

[default.package]
[default.package.parameters]
resolve_s3 = true

[default.sync]
[default.sync.parameters]
watch = true

[default.local_start_api]
[default.local_start_api.parameters]
warm_containers = "EAGER"

[default.local_start_lambda]
[default.local_start_lambda.parameters]
warm_containers = "EAGER"

注目すべきは、以下の記載箇所です

1
2
3
4
5
6
7
8
9
10
[default.deploy]
[default.deploy.parameters]
capabilities = "CAPABILITY_IAM"
confirm_changeset = true
resolve_s3 = true
resolve_image_repos = true
s3_bucket = "aws-sam-cli-managed-default-samclisourcebucket-1234567890"
s3_prefix = "sam-app"
region = "ap-northeast-1"
image_repositories = ["HelloWorldFunction=123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/samapp2b0162595/helloworldfunction19d43fc4repo"]

重複不可のパラメータ

最初に記載した通りですが、エラーメッセージは以下の通りです。

1
Error: Only one of the following can be provided: '--image-repositories', '--image-repository', or '--resolve-image-repos'. Do you have multiple specified in the command or in a configuration file?

‘–image-repositories’、’–image-repository’、’–resolve-image-repos’は、
どれか1個だけを指定できますが、samconfig.toml には重複して記載されていました。

1
2
3
4
5
6
[default.deploy]
[default.deploy.parameters]
(中略)
resolve_image_repos = true
(中略)
image_repositories = ["HelloWorldFunction=123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/samapp2b0162595/helloworldfunction19d43fc4repo"]

resolve_image_repos = true が不要なので削除します。

これを削除して、sam deployをすると、今度はs3でエラーが出るかと思います。

1
2
$ sam deploy
Error: Cannot use both --resolve-s3 and --s3-bucket parameters in non-guided deployments. Please use only one or use the --guided option for a guided deployment.

こちらも “resolve_s3 = true” が不要なので削除します。

最終的に、samconfig.toml を以下のようにしました。

1
2
3
4
5
6
7
8
[default.deploy]
[default.deploy.parameters]
capabilities = "CAPABILITY_IAM"
confirm_changeset = true
s3_bucket = "aws-sam-cli-managed-default-samclisourcebucket-1234567890"
s3_prefix = "sam-app"
region = "ap-northeast-1"
image_repositories = ["HelloWorldFunction=123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/samapp2b0162595/helloworldfunction19d43fc4repo"]

これで、sam deploy が通るようになりました。

まとめ

samconfig.tomlをよく見ている人はすぐに気が付くのかもしれませんが、
私はまだSAMを触り始めたばかりだったのでエラーメッセージの原因を見つけるのに時間がかかりました。

1回目のsam deployは成功するのに、2回目以降は成功しないことから、
何度かsam initからやり直して変化するファイルを探してようやくエラーメッセージの意味を理解しました。

この記事が同様に詰まっている方の助けになれば幸いです。