Last active
August 16, 2023 07:54
-
-
Save ijin/db027846fefb339187e3f2833fe2d034 to your computer and use it in GitHub Desktop.
[Terraform] API Gateway to get Elastic Beanstalk IP(s)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| provider "aws" { | |
| region = "ap-northeast-1" | |
| allowed_account_ids = ["${var.names.account_id}"] | |
| } | |
| variable "names" { | |
| default = { | |
| account_id = "$ACCOUNT_ID" | |
| } | |
| } | |
| # REST API | |
| resource "aws_api_gateway_rest_api" "EB" { | |
| name = "EB" | |
| description = "get EB info" | |
| } | |
| # Resources | |
| resource "aws_api_gateway_resource" "eb" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| parent_id = "${aws_api_gateway_rest_api.EB.root_resource_id}" | |
| path_part = "eb" | |
| } | |
| resource "aws_api_gateway_resource" "env_name" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| parent_id = "${aws_api_gateway_resource.eb.id}" | |
| path_part = "{env_name}" | |
| } | |
| resource "aws_api_gateway_resource" "ip" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| parent_id = "${aws_api_gateway_resource.env_name.id}" | |
| path_part = "ip" | |
| } | |
| resource "aws_api_gateway_resource" "server_num" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| parent_id = "${aws_api_gateway_resource.ip.id}" | |
| path_part = "{server_num}" | |
| } | |
| # Methods | |
| resource "aws_api_gateway_method" "ip_get" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| resource_id = "${aws_api_gateway_resource.ip.id}" | |
| http_method = "GET" | |
| authorization = "NONE" | |
| } | |
| resource "aws_api_gateway_method" "server_num_get" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| resource_id = "${aws_api_gateway_resource.server_num.id}" | |
| http_method = "GET" | |
| authorization = "NONE" | |
| } | |
| # Integrations | |
| resource "aws_api_gateway_integration" "ip_get" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| resource_id = "${aws_api_gateway_resource.ip.id}" | |
| http_method = "${aws_api_gateway_method.ip_get.http_method}" | |
| integration_http_method = "POST" | |
| type = "AWS" | |
| uri = "arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:${var.names.account_id}:function:eb_ip/invocations" | |
| request_templates = { | |
| "application/json" = "{ \"env_name\": \"$input.params('env_name')\" }" | |
| } | |
| depends_on = ["aws_api_gateway_method.ip_get"] | |
| } | |
| resource "aws_api_gateway_integration" "server_num_get" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| resource_id = "${aws_api_gateway_resource.server_num.id}" | |
| http_method = "${aws_api_gateway_method.server_num_get.http_method}" | |
| integration_http_method = "POST" | |
| type = "AWS" | |
| uri = "arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:${var.names.account_id}:function:eb_ip/invocations" | |
| request_templates = { | |
| "application/json" = "{\n \"env_name\": \"$input.params('env_name')\",\n \"server_num\": \"$input.params('server_num')\" \n}" | |
| } | |
| depends_on = ["aws_api_gateway_method.server_num_get"] | |
| } | |
| # Method responses | |
| resource "aws_api_gateway_method_response" "ip_200" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| resource_id = "${aws_api_gateway_resource.ip.id}" | |
| http_method = "${aws_api_gateway_method.ip_get.http_method}" | |
| status_code = "200" | |
| } | |
| resource "aws_api_gateway_method_response" "ip_400" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| resource_id = "${aws_api_gateway_resource.ip.id}" | |
| http_method = "${aws_api_gateway_method.ip_get.http_method}" | |
| status_code = "400" | |
| depends_on = ["aws_api_gateway_method_response.ip_200"] | |
| } | |
| resource "aws_api_gateway_method_response" "server_num_200" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| resource_id = "${aws_api_gateway_resource.server_num.id}" | |
| http_method = "${aws_api_gateway_method.server_num_get.http_method}" | |
| status_code = "200" | |
| } | |
| resource "aws_api_gateway_method_response" "server_num_400" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| resource_id = "${aws_api_gateway_resource.server_num.id}" | |
| http_method = "${aws_api_gateway_method.server_num_get.http_method}" | |
| status_code = "400" | |
| depends_on = ["aws_api_gateway_method_response.server_num_200"] | |
| } | |
| # Integration responses | |
| resource "aws_api_gateway_integration_response" "ip_get_200" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| resource_id = "${aws_api_gateway_resource.ip.id}" | |
| http_method = "${aws_api_gateway_method.ip_get.http_method}" | |
| status_code = "${aws_api_gateway_method_response.ip_200.status_code}" | |
| response_templates = { | |
| "application/json" = "$input.path('$')" | |
| } | |
| } | |
| resource "aws_api_gateway_integration_response" "ip_get_400" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| resource_id = "${aws_api_gateway_resource.ip.id}" | |
| http_method = "${aws_api_gateway_method.ip_get.http_method}" | |
| status_code = "${aws_api_gateway_method_response.ip_400.status_code}" | |
| selection_pattern = "[^0-9](.|\n)*" | |
| response_templates = { | |
| "application/json" = "$input.path('$').errorMessage" | |
| } | |
| depends_on = ["aws_api_gateway_integration_response.ip_get_200"] | |
| } | |
| resource "aws_api_gateway_integration_response" "server_num_get_200" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| resource_id = "${aws_api_gateway_resource.server_num.id}" | |
| http_method = "${aws_api_gateway_method.server_num_get.http_method}" | |
| status_code = "${aws_api_gateway_method_response.server_num_200.status_code}" | |
| response_templates = { | |
| "application/json" = "$input.path('$')" | |
| } | |
| } | |
| resource "aws_api_gateway_integration_response" "server_num_get_400" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| resource_id = "${aws_api_gateway_resource.server_num.id}" | |
| http_method = "${aws_api_gateway_method.server_num_get.http_method}" | |
| status_code = "${aws_api_gateway_method_response.server_num_400.status_code}" | |
| selection_pattern = "[^0-9](.|\n)*" | |
| response_templates = { | |
| "application/json" = "$input.path('$').errorMessage" | |
| } | |
| depends_on = ["aws_api_gateway_integration_response.server_num_get_200"] | |
| } | |
| resource "aws_api_gateway_deployment" "eb_deployment" { | |
| rest_api_id = "${aws_api_gateway_rest_api.EB.id}" | |
| depends_on = ["aws_api_gateway_integration.ip_get"] | |
| stage_name = "prod" | |
| } | |
| # Permissions | |
| #resource "aws_lambda_permission" "apigw_ip" { | |
| # statement_id = "apigw_for_ip" | |
| # action = "lambda:InvokeFunction" | |
| # function_name = "eb_ip" | |
| # principal = "apigateway.amazonaws.com" | |
| # source_arn = "arn:aws:execute-api:ap-northeast-1:${var.names.account_id}:${aws_api_gateway_rest_api.EB.id}/*/GET/eb/*/${aws_api_gateway_resource.ip.path_part}" | |
| #} | |
| #resource "aws_lambda_permission" "apigw_server_num" { | |
| # statement_id = "apigw_for_server_num" | |
| # action = "lambda:InvokeFunction" | |
| # function_name = "eb_ip" | |
| # principal = "apigateway.amazonaws.com" | |
| # source_arn = "arn:aws:execute-api:ap-northeast-1:${var.names.account_id}:${aws_api_gateway_rest_api.EB.id}/*/GET/eb/*/${aws_api_gateway_resource.ip.path_part}/*" | |
| #} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment