Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

amazon web services - How to refresh AWS Lambda permission for API Gateway using Terraform?

I am deploying a REST API Gateway using Terraform. Couple of endpoints are accessing Lambda function to return response. Whenever I deploy api-gw using terraform, the Lambda permission doesn't seem to refresh and I have to manually open the api-gw portal in AWS console and again add that lambda function post which it prompts me to allow invoke action. How can I refresh the permission without having to do these manual steps ? I am using below snippet for api-gw deployment and lambda permissions:

resource "aws_api_gateway_deployment" "deploy" {
  rest_api_id = aws_api_gateway_rest_api.apigw.id
  stage_name  = ""
  variables  = {
    deployed_at = timestamp()
  }
  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_lambda_permission" "customers_lambda_permission" {
  statement_id  = "AllowDemoAPIInvokeProjectGet"
  action        = "lambda:InvokeFunction"
  function_name = local.lambda_name
  principal     = "apigateway.amazonaws.com"
  source_arn = "${aws_api_gateway_rest_api.apigw.execution_arn}/*/GET/api/customers"

}
question from:https://stackoverflow.com/questions/65914380/how-to-refresh-aws-lambda-permission-for-api-gateway-using-terraform

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your aws_api_gateway_deployment resource should depend on the aws_api_gateway_integration so that the lambda integration is created before deployment.

resource "aws_api_gateway_deployment" "deploy" {
  ...
  depends_on = [
    aws_api_gateway_integration.example1,
    aws_api_gateway_integration.example2
  ]
}

or use triggers attribute:

resource "aws_api_gateway_deployment" "deploy" {
  ...
  triggers = {
    redeployment = sha1(jsonencode([
      aws_api_gateway_resource.example1.id,
      aws_api_gateway_method.example1.id,
      aws_api_gateway_integration.example1.id,
    ]))
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...