はじめに
Terraformを使うことで、環境構築を自動化することはとても便利だと思います。 Terraform自体はカスタマイズ性が高く、できることが多いですが、エラー解決に工夫する場合も多く出てきます。
エラー
既にAWSにデプロイしたAWS Lambda
を使っているアプリをもとのterraform.tfstate
を持っていない状態で、新しい環境で既存リソースをインポートして再デプロイする際にエラーが出てました。
Message_: "The statement id (AllowExecutionFromAPIGateway) provided already exists. Please provide a new statement id, or remove the existing statement.",
│ Type: "User"
│ }
│
│ with module.web_base_lambda_function.aws_lambda_permission.unqualified_alias_triggers["AllowExecutionFromAPIGateway"],
│ on .terraform/modules/web_base_lambda_function/main.tf line 259, in resource "aws_lambda_permission" "unqualified_alias_triggers":
│ 259: resource "aws_lambda_permission" "unqualified_alias_triggers" {
│
ResourceConflictException: The statement id (AllowExecutionFromAPIGateway) provided already exists.
Please provide a new statement id, or remove the existing statement.
["AllowExecutionFromAPIGateway"]: Still creating... [6m16s elapsed]
module.web_base_lambda_function.aws_lambda_permission.unqualified_alias_triggers["AllowExecutionFromAPIGateway"]: Still creating... [6m26s elapsed]
module.web_base_lambda_function.aws_lambda_permission.unqualified_alias_triggers["AllowExecutionFromAPIGateway"]: Still creating... [6m36s elapsed]
解決案
main.tf
にあるresource "aws_lambda_permission" "unqualified_alias_triggers"
の部分は、自動的にLambdaのpermission AllowExecutionFromAPIGateway を作成されます。
Please provide a new statement id, or remove the existing statement ということで、AllowExecutionFromAPIGateway を削除するか、新たにstatement idを指定するか二択しかないらしい。
色々調べて、やはりこれをスキップすることは難しいと思っていて(最初からインポートに問題あったかもしれませんが)、解決案としては、AWSコンソールに入って、Lambda -> [my_lambda] -> configuration -> permissions の「Resource-based policy statements」とのセクションにある AllowExecutionFromAPIGateway を削除してから、再度terraform applyでデプロイすれば解決されます。
以上。