Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 39 additions & 39 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log
crash.*.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Ignore transient lock info files created by terraform apply
.terraform.tfstate.lock.info

# Include override files you do wish to add to version control using negated pattern
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
.terraformrc
terraform.rc

*.zip
# Local .terraform directories
**/.terraform/*
# .tfstate files
*.tfstate
*.tfstate.*
# Crash log files
crash.log
crash.*.log
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json
# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json
# Ignore transient lock info files created by terraform apply
.terraform.tfstate.lock.info
# Include override files you do wish to add to version control using negated pattern
# !example_override.tf
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*
# Ignore CLI configuration files
.terraformrc
terraform.rc
*.zip
5 changes: 4 additions & 1 deletion .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 28 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
# Commands to invoke api
```bash
# Add movie
INVOKE_URL=https://xxxxxxx.amazonaws.com
curl \
-X PUT \
-H "Content-Type: application/json" \
-d '{"year": "2013", "title": "The Amazing Spider"}' \
${INVOKE_URL}/topmovies

# Get movie for a particular year
curl ${INVOKE_URL}/topmovies/2013

# Get listing
curl ${INVOKE_URL}/topmovies

# Delete movie for a particular year
curl -X DELETE ${INVOKE_URL}/topmovies/2013
```
# Commands to invoke api
```bash
# Add movie
INVOKE_URL=https://xxxxxxx.amazonaws.com
curl \
-X PUT \
-H "Content-Type: application/json" \
-d '{"year": "2025", "title": "The Avengers"}' \
${INVOKE_URL}/topmovies

# Get movie for a particular year
curl ${INVOKE_URL}/topmovies/2013

# Get listing
curl ${INVOKE_URL}/topmovies

# Delete movie for a particular year
curl -X DELETE ${INVOKE_URL}/topmovies/2013
```


# Missing dependancy Error:
![alt text](image-1.png)

Resolution:
![alt text](image.png)


12 changes: 12 additions & 0 deletions acm.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "aws_acm_certificate" "custom_domain_cert" {
domain_name = "ws-api.sctp-sandbox.com" # Replace with your custom domain
validation_method = "DNS"
subject_alternative_names = [
"www.ws-api.sctp-sandbox.com" # Optional: add any additional subdomains
]

tags = {
Name = "CustomDomainCertificate"
}
}

153 changes: 108 additions & 45 deletions apigateway.tf
Original file line number Diff line number Diff line change
@@ -1,45 +1,108 @@
resource "aws_apigatewayv2_api" "http_api" {
name = "${local.name_prefix}-topmovies-api"
protocol_type = "HTTP"
}

resource "aws_apigatewayv2_stage" "default" {
api_id = aws_apigatewayv2_api.http_api.id

name = "$default"
auto_deploy = true
}

resource "aws_apigatewayv2_integration" "apigw_lambda" {
api_id = aws_apigatewayv2_api.http_api.id

integration_uri = "" # todo: fill with apporpriate value
integration_type = "AWS_PROXY"
integration_method = "POST"
payload_format_version = "2.0"
}

# resource "aws_apigatewayv2_route" "get_topmovies" {
# # todo: fill with apporpriate value
# }

# resource "aws_apigatewayv2_route" "get_topmovies_by_year" {
# # todo: fill with apporpriate value
# }

# resource "aws_apigatewayv2_route" "put_topmovies" {
# # todo: fill with apporpriate value
# }

# resource "aws_apigatewayv2_route" "delete_topmovies_by_year" {
# # todo: fill with apporpriate value
# }

resource "aws_lambda_permission" "api_gw" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.http_api_lambda.function_name
principal = "apigateway.amazonaws.com"

source_arn = "${aws_apigatewayv2_api.http_api.execution_arn}/*/*"
}
# Creates an AWS API Gateway v2 (HTTP API)
resource "aws_apigatewayv2_api" "http_api" {
name = "${local.name_prefix}-topmovies-api"
protocol_type = "HTTP"
}

# Define the API Gateway stage with logging
resource "aws_apigatewayv2_stage" "default" {
api_id = aws_apigatewayv2_api.http_api.id
name = "$default"
auto_deploy = true
}

# Intergrate API Gateway with Lambda function
resource "aws_apigatewayv2_integration" "apigw_lambda" {
api_id = aws_apigatewayv2_api.http_api.id
integration_uri = aws_lambda_function.http_api_lambda.invoke_arn
integration_type = "AWS_PROXY"
integration_method = "POST"
payload_format_version = "2.0"
}

# Create API routes:
# GET /topmovies
resource "aws_apigatewayv2_route" "get_topmovies" {
api_id = aws_apigatewayv2_api.http_api.id
route_key = "GET /topmovies"
target = "integrations/${aws_apigatewayv2_integration.apigw_lambda.id}"
}

# GET /topmovies/{year}
resource "aws_apigatewayv2_route" "get_topmovies_by_year" {
api_id = aws_apigatewayv2_api.http_api.id
route_key = "GET /topmovies/{year}"
target = "integrations/${aws_apigatewayv2_integration.apigw_lambda.id}"
}

# PUT /topmovies
resource "aws_apigatewayv2_route" "put_topmovies" {
api_id = aws_apigatewayv2_api.http_api.id
route_key = "PUT /topmovies"
target = "integrations/${aws_apigatewayv2_integration.apigw_lambda.id}"
}

# DELETE /topmovies/{year}
resource "aws_apigatewayv2_route" "delete_topmovies_by_year" {
api_id = aws_apigatewayv2_api.http_api.id
route_key = "DELETE /topmovies/{year}"
target = "integrations/${aws_apigatewayv2_integration.apigw_lambda.id}"
}

# Grants API Gateway permission to invoke the Lambda function.
resource "aws_lambda_permission" "api_gw" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.http_api_lambda.function_name
principal = "apigateway.amazonaws.com"

source_arn = "${aws_apigatewayv2_api.http_api.execution_arn}/*/*"
}

# Create API Gateway custom domain
resource "aws_apigatewayv2_domain_name" "custom_domain" {
domain_name = "ws-api.sctp-sandbox.com" # Replace with your custom domain

domain_name_configuration {
certificate_arn = aws_acm_certificate.custom_domain_cert.arn
endpoint_type = "REGIONAL"
security_policy = "TLS_1_2"
}
depends_on = [aws_acm_certificate_validation.cert_validation]
}

# API Gateway mapping to custom domain
resource "aws_apigatewayv2_api_mapping" "default" {
api_id = aws_apigatewayv2_api.http_api.id
domain_name = aws_apigatewayv2_domain_name.custom_domain.domain_name
stage = aws_apigatewayv2_stage.default.name
}

# CloudWatch log group for API Gateway with retention of 7 days
resource "aws_cloudwatch_log_group" "api_gateway_log_group" {
name = "/aws/apigateway/${aws_apigatewayv2_api.http_api.id}"
retention_in_days = 7
}





# # Alternative for API route
# # API Routes
# locals {
# routes = [
# { method = "GET", path = "/topmovies" },
# { method = "GET", path = "/topmovies/{year}" },
# { method = "PUT", path = "/topmovies" },
# { method = "DELETE", path = "/topmovies/{year}" }
# ]
# }

# resource "aws_apigatewayv2_route" "routes" {
# for_each = { for route in local.routes : "${route.method} ${route.path}" => route }

# api_id = aws_apigatewayv2_api.http_api.id
# route_key = "${each.value.method} ${each.value.path}"
# target = "integrations/${aws_apigatewayv2_integration.apigw_lambda.id}"
# }
22 changes: 11 additions & 11 deletions dynamodb.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
resource "aws_dynamodb_table" "table" {
name = "${local.name_prefix}-topmovies"
billing_mode = "PAY_PER_REQUEST"
hash_key = "year"

attribute {
name = "year"
type = "" # todo: fill with apporpriate value
}

}
resource "aws_dynamodb_table" "table" {
name = "${local.name_prefix}-topmovies"
billing_mode = "PAY_PER_REQUEST"
hash_key = "year"
attribute {
name = "year"
type = "N" # todo: fill with apporpriate value
}
}
Binary file added image-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading