Skip to content

Instantly share code, notes, and snippets.

@yuya-takeyama
Created January 29, 2022 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuya-takeyama/61977cc39a5eb8d1bc83dd861a8687d0 to your computer and use it in GitHub Desktop.
Save yuya-takeyama/61977cc39a5eb8d1bc83dd861a8687d0 to your computer and use it in GitHub Desktop.
Example of sending an HTTP request from Step Functions via API Gateway
module "http-get" {
source = "./modules/http-caller"
method = "GET"
uri = "https://httpbin.org/get"
}
module "http-post" {
source = "./modules/http-caller"
method = "POST"
uri = "https://httpbin.org/post"
}
module "http-yuyat" {
source = "./modules/http-caller"
uri = "https://yuyat.jp"
}
resource "aws_iam_role" "main" {
name = "StepFunctions-CallHTTP"
assume_role_policy = data.aws_iam_policy_document.assume-role-policy.json
}
data "aws_iam_policy_document" "assume-role-policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["states.amazonaws.com"]
}
}
}
resource "aws_sfn_state_machine" "main" {
name = "CallHTTP"
role_arn = aws_iam_role.main.arn
definition = jsonencode({
StartAt = "Parallel",
States = {
Parallel = {
Type = "Parallel",
Branches = [
{
StartAt = "GET",
States = {
GET = {
Type = "Task",
Resource = "arn:aws:states:::apigateway:invoke",
Parameters = {
ApiEndpoint = module.http-get.api_endpoint,
Method = module.http-get.method,
Stage = module.http-get.stage,
Path = module.http-get.path,
QueryParameters = {
foo = [
"bar"
],
"hoge" = [
"fuga"
]
},
AuthType = "NO_AUTH"
},
End = true
}
}
},
{
StartAt = "POST",
States = {
POST = {
Type = "Task",
Resource = "arn:aws:states:::apigateway:invoke",
Parameters = {
ApiEndpoint = module.http-post.api_endpoint,
Method = module.http-post.method,
Stage = module.http-post.stage,
Path = module.http-post.path,
Headers = {
Content-Type = [
"application/json"
]
},
QueryParameters = {
"QueryParameter1" = [
"QueryParameterValue1"
],
QueryParameter2 = [
"QueryParameterValue2",
"QueryParameterValue3"
]
},
RequestBody = { foo = "bar" },
AuthType = "NO_AUTH"
},
End = true
}
}
},
{
StartAt = "YUYAT",
States = {
YUYAT = {
Type = "Task",
Resource = "arn:aws:states:::apigateway:invoke",
Parameters = {
ApiEndpoint = module.http-yuyat.api_endpoint,
Method = module.http-yuyat.method,
Stage = module.http-yuyat.stage,
Path = module.http-yuyat.path,
AuthType = "NO_AUTH"
},
End = true
}
}
}
],
End = true
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment