Skip to content

Commit

Permalink
missed some saves
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakeasaurus committed Dec 3, 2024
1 parent d227b07 commit e0d1143
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 203 deletions.
120 changes: 14 additions & 106 deletions modules/aws/api_gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
<img src="/images/terraform_modules_logo.webp" alt="Logo" width="300" height="300">
</a>

<h3 align="center">EC2 Instance Module</h3>
<h3 align="center">API Gateway</h3>
<p align="center">
This module sets up an EC2 instance with the parameters specified. This module has root block devices modifiable
This module creates an AWS API Gateway v2 configuration.
<br />
<a href="https://github.com/zachreborn/terraform-modules"><strong>Explore the docs »</strong></a>
<br />
Expand Down Expand Up @@ -62,69 +62,14 @@

<!-- USAGE EXAMPLES -->
## Usage
### Simple EC2 Instance Example
### Basic HTTP API Gateway Example
This example creates a basic HTTP API Gateway.
```
module "aws_prod_app" {
source = "github.com/zachreborn/terraform-modules//modules/aws/ec2_instance"
ami = "ami-ffffffff"
availability_zone = module.vpc.availability_zone[0]
count = 1
instance_type = "m5.xlarge"
key_name = module.keypair.key_name
name = "aws_prod_app"
subnet_id = module.vpc.private_subnet_ids[0]
vpc_security_group_ids = ["sg-ffffffff"]
tags = {
terraform = "true"
created_by = "YOUR NAME"
environment = "prod"
role = "app"
backup = "true"
}
}
```

### EC2 Instance With Second Attached EBS Volume
```
module "app_server" {
source = "github.com/zachreborn/terraform-modules//modules/aws/ec2_instance"
name = "app_server"
ami = "ami-ffffffff"
count = 1
availability_zone = module.vpc.availability_zone[0]
subnet_id = module.vpc.private_subnet_ids[0]
instance_type = "t2.large"
key_name = module.keypair.key_name
vpc_security_group_ids = module.app_server_sg.id
root_volume_size = "100"
tags = {
terraform = "true"
created_by = "terraform"
environment = "prod"
role = "app_server"
backup = "true"
}
}
module "example_api_gateway" {
source = "github.com/zachreborn/terraform-modules//modules/aws/api_gateway"
module "app_server_d_drive" {
source = "github.com/zachreborn/terraform-modules//modules/aws/ebs_volume"
availability_zone = module.vpc.availability_zone[0]
size = "50"
device_name = "xvdb"
instance_id = module.app_server.id[0]
tags = {
Name = "app_server"
os_drive = "d"
device_name = "xvdb"
terraform = "true"
created_by = "terraform"
environment = "prod"
role = "app_server"
backup = "true"
}
name = "example-api"
protocol_type = "HTTP"
}
```

Expand Down Expand Up @@ -156,60 +101,23 @@ No modules.

| Name | Type |
|------|------|
| [aws_cloudwatch_metric_alarm.instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
| [aws_cloudwatch_metric_alarm.system](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
| [aws_instance.ec2](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | resource |
| [aws_apigatewayv2_api.example](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_api) | resource |
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_ami"></a> [ami](#input\_ami) | (Optional) AMI to use for the instance. Required unless launch\_template is specified and the Launch Template specifes an AMI. If an AMI is specified in the Launch Template, setting ami will override the AMI specified in the Launch Template. | `string` | n/a | yes |
| <a name="input_associate_public_ip_address"></a> [associate\_public\_ip\_address](#input\_associate\_public\_ip\_address) | If true, the EC2 instance will have associated public IP address | `bool` | `false` | no |
| <a name="input_auto_recovery"></a> [auto\_recovery](#input\_auto\_recovery) | (Optional) Whether the instance is protected from auto recovery by Auto Recovery from User Space (ARU) feature. Can be 'default' or 'disabled'. Defaults to default. See Auto Recovery from User Space for more information. https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-auto-recovery.html | `string` | `"default"` | no |
| <a name="input_availability_zone"></a> [availability\_zone](#input\_availability\_zone) | The AZ to start the instance in | `string` | `""` | no |
| <a name="input_disable_api_termination"></a> [disable\_api\_termination](#input\_disable\_api\_termination) | If true, enables EC2 Instance Termination Protection | `bool` | `false` | no |
| <a name="input_ebs_optimized"></a> [ebs\_optimized](#input\_ebs\_optimized) | If true, the launched EC2 instance will be EBS-optimized | `bool` | `false` | no |
| <a name="input_encrypted"></a> [encrypted](#input\_encrypted) | (Optional) Enable volume encryption. (Default: false). Must be configured to perform drift detection. | `bool` | `true` | no |
| <a name="input_http_endpoint"></a> [http\_endpoint](#input\_http\_endpoint) | (Optional) Whether the metadata service is available. Valid values include enabled or disabled. Defaults to enabled. | `string` | `"enabled"` | no |
| <a name="input_http_tokens"></a> [http\_tokens](#input\_http\_tokens) | (Optional) Whether or not the metadata service requires session tokens, also referred to as Instance Metadata Service Version 2 (IMDSv2). Valid values include optional or required. Defaults to optional. | `string` | `"required"` | no |
| <a name="input_iam_instance_profile"></a> [iam\_instance\_profile](#input\_iam\_instance\_profile) | (Optional) IAM Instance Profile to launch the instance with. Specified as the name of the Instance Profile. Ensure your credentials have the correct permission to assign the instance profile according to the EC2 documentation, notably iam:PassRole. | `string` | `null` | no |
| <a name="input_instance_initiated_shutdown_behavior"></a> [instance\_initiated\_shutdown\_behavior](#input\_instance\_initiated\_shutdown\_behavior) | (Optional) Shutdown behavior for the instance. Amazon defaults this to stop for EBS-backed instances and terminate for instance-store instances. Cannot be set on instance-store instances. See Shutdown Behavior for more information. https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/terminating-instances.html#Using_ChangingInstanceInitiatedShutdownBehavior | `string` | `"stop"` | no |
| <a name="input_instance_type"></a> [instance\_type](#input\_instance\_type) | The AWS instance type to utilize for the specifications of the instance | `string` | n/a | yes |
| <a name="input_ipv6_addresses"></a> [ipv6\_addresses](#input\_ipv6\_addresses) | Specify one or more IPv6 addresses from the range of the subnet to associate with the primary network interface | `list(string)` | `[]` | no |
| <a name="input_key_name"></a> [key\_name](#input\_key\_name) | The key name to use for the instance | `string` | `""` | no |
| <a name="input_monitoring"></a> [monitoring](#input\_monitoring) | If true, the launched EC2 instance will have detailed monitoring enabled | `bool` | `false` | no |
| <a name="input_name"></a> [name](#input\_name) | Name to be used on all resources as prefix | `string` | n/a | yes |
| <a name="input_number"></a> [number](#input\_number) | Number of instances to launch | `number` | `1` | no |
| <a name="input_placement_group"></a> [placement\_group](#input\_placement\_group) | The Placement Group to start the instance in | `string` | `""` | no |
| <a name="input_private_ip"></a> [private\_ip](#input\_private\_ip) | Private IP address to associate with the instance in a VPC | `string` | `null` | no |
| <a name="input_root_delete_on_termination"></a> [root\_delete\_on\_termination](#input\_root\_delete\_on\_termination) | (Optional) Whether the volume should be destroyed on instance termination (Default: true) | `bool` | `true` | no |
| <a name="input_root_volume_size"></a> [root\_volume\_size](#input\_root\_volume\_size) | (Optional) The size of the volume in gigabytes. | `string` | `"100"` | no |
| <a name="input_root_volume_type"></a> [root\_volume\_type](#input\_root\_volume\_type) | (Optional) Type of volume. Valid values include standard, gp2, gp3, io1, io2, sc1, or st1. Defaults to gp3. | `string` | `"gp3"` | no |
| <a name="input_source_dest_check"></a> [source\_dest\_check](#input\_source\_dest\_check) | Controls if traffic is routed to the instance when the destination address does not match the instance. Used for NAT or VPNs. | `bool` | `true` | no |
| <a name="input_subnet_id"></a> [subnet\_id](#input\_subnet\_id) | The VPC Subnet ID to launch in | `string` | `""` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | A mapping of tags to assign to the resource | `map(string)` | `{}` | no |
| <a name="input_tenancy"></a> [tenancy](#input\_tenancy) | The tenancy of the instance (if the instance is running in a VPC). Available values: default, dedicated, host. | `string` | `"default"` | no |
| <a name="input_user_data"></a> [user\_data](#input\_user\_data) | The user data to provide when launching the instance | `string` | `""` | no |
| <a name="input_vpc_security_group_ids"></a> [vpc\_security\_group\_ids](#input\_vpc\_security\_group\_ids) | A list of security group IDs to associate with | `list(any)` | n/a | yes |
| <a name="input_name"></a> [name](#input\_name) | (Required) The name of the API | `string` | n/a | yes |
| <a name="input_protocol_type"></a> [protocol_type](#input\_protocol_type) | (Required) The API protocol. Valid values: HTTP, WEBSOCKET | `string` | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_availability_zone"></a> [availability\_zone](#output\_availability\_zone) | List of availability zones of instances |
| <a name="output_id"></a> [id](#output\_id) | List of IDs of instances |
| <a name="output_key_name"></a> [key\_name](#output\_key\_name) | List of key names of instances |
| <a name="output_primary_network_interface_id"></a> [primary\_network\_interface\_id](#output\_primary\_network\_interface\_id) | List of IDs of the primary network interface of instances |
| <a name="output_private_dns"></a> [private\_dns](#output\_private\_dns) | List of private DNS names assigned to the instances. Can only be used inside the Amazon EC2, and only available if you've enabled DNS hostnames for your VPC |
| <a name="output_private_ip"></a> [private\_ip](#output\_private\_ip) | List of private IP addresses assigned to the instances |
| <a name="output_public_dns"></a> [public\_dns](#output\_public\_dns) | List of public DNS names assigned to the instances. For EC2-VPC, this is only available if you've enabled DNS hostnames for your VPC |
| <a name="output_public_ip"></a> [public\_ip](#output\_public\_ip) | List of public IP addresses assigned to the instances, if applicable |
| <a name="output_security_groups"></a> [security\_groups](#output\_security\_groups) | List of associated security groups of instances |
| <a name="output_subnet_id"></a> [subnet\_id](#output\_subnet\_id) | List of IDs of VPC subnets of instances |
| <a name="output_vpc_security_group_ids"></a> [vpc\_security\_group\_ids](#output\_vpc\_security\_group\_ids) | List of associated security groups of instances, if running in non-default VPC |
| <a name="output_api_endpoint"></a> [api\_endpoint](#output\_api\_endpoint) | The URI of the API |
| <a name="output_api_id"></a> [api\_id](#output\_api\_id) | The API identifier |
<!-- END_TF_DOCS -->

<!-- LICENSE -->
Expand Down Expand Up @@ -257,4 +165,4 @@ Project Link: [https://github.com/zachreborn/terraform-modules](https://github.c
[linkedin-url]: https://www.linkedin.com/in/zachary-hill-5524257a/
[product-screenshot]: /images/screenshot.webp
[Terraform.io]: https://img.shields.io/badge/Terraform-7B42BC?style=for-the-badge&logo=terraform
[Terraform-url]: https://terraform.io
[Terraform-url]: https://terraform.io
77 changes: 20 additions & 57 deletions modules/aws/api_gateway/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,28 @@ data "aws_region" "current" {}
# API Gateway
#############################
resource "aws_apigatewayv2_api" "example" {
name = var.name
#Required
name = var.name
protocol_type = var.protocol_type
route_selection_expression = "$request.body.action"
}

###################################################
CloudWatch Alarms
###################################################

#####################
# Status Check Failed Instance Metric
#####################

resource "aws_cloudwatch_metric_alarm" "instance" {
actions_enabled = true
alarm_actions = []
alarm_description = "EC2 instance StatusCheckFailed_Instance alarm"
alarm_name = format("%s-instance-alarm", aws_instance.ec2[count.index].id)
comparison_operator = "GreaterThanOrEqualToThreshold"
count = var.number
datapoints_to_alarm = 2
dimensions = {
InstanceId = aws_instance.ec2[count.index].id
}
evaluation_periods = "2"
insufficient_data_actions = []
metric_name = "StatusCheckFailed_Instance"
namespace = "AWS/EC2"
ok_actions = []
period = "60"
statistic = "Maximum"
threshold = "1"
treat_missing_data = "missing"
#unit = var.unit
}

#####################
# Status Check Failed System Metric
#####################
#Optional
api_key_selection_expression = var.api_key_selection_expression
cors_configuration {
allow_credentials = lookup(var.cors_configuration, "allow_credentials", null)
allow_headers = lookup(var.cors_configuration, "allow_headers", null)
allow_methods = lookup(var.cors_configuration, "allow_methods", null)
allow_origins = lookup(var.cors_configuration, "allow_origins", null)
expose_headers = lookup(var.cors_configuration, "expose_headers", null)
max_age = lookup(var.cors_configuration, "max_age", null)
}
credentials_arn = var.credentials_arn
description = var.description
disable_execute_api_endpoint = var.disable_execute_api_endpoint
fail_on_warnings = var.fail_on_warnings
tags = var.tags
target = var.target
version = var.version
body = var.body

resource "aws_cloudwatch_metric_alarm" "system" {
actions_enabled = true
alarm_actions = ["arn:aws:automate:${data.aws_region.current.name}:ec2:recover"]
alarm_description = "EC2 instance StatusCheckFailed_System alarm"
alarm_name = format("%s-system-alarm", aws_instance.ec2[count.index].id)
comparison_operator = "GreaterThanOrEqualToThreshold"
count = var.number
datapoints_to_alarm = 2
dimensions = {
InstanceId = aws_instance.ec2[count.index].id
}
evaluation_periods = "2"
insufficient_data_actions = []
metric_name = "StatusCheckFailed_System"
namespace = "AWS/EC2"
ok_actions = []
period = "60"
statistic = "Maximum"
threshold = "1"
treat_missing_data = "missing"
#unit = var.unit
}
60 changes: 20 additions & 40 deletions modules/aws/api_gateway/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,54 +1,34 @@
output "id" {
description = "List of IDs of instances"
value = aws_instance.ec2[*].id
description = "The API identifier"
value = aws_apigatewayv2_api.example.id
}

output "availability_zone" {
description = "List of availability zones of instances"
value = aws_instance.ec2[*].availability_zone
output "api_endpoint" {
description = "The URI of the API"
value = aws_apigatewayv2_api.example.api_endpoint
}

output "key_name" {
description = "List of key names of instances"
value = aws_instance.ec2[*].key_name
output "arn" {
description = "The ARN of the API"
value = aws_apigatewayv2_api.example.arn
}

output "public_dns" {
description = "List of public DNS names assigned to the instances. For EC2-VPC, this is only available if you've enabled DNS hostnames for your VPC"
value = aws_instance.ec2[*].public_dns
output "execution_arn" {
description = "The ARN prefix to be used in permission policies"
value = aws_apigatewayv2_api.example.execution_arn
}

output "public_ip" {
description = "List of public IP addresses assigned to the instances, if applicable"
value = aws_instance.ec2[*].public_ip
output "api_key_selection_expression" {
description = "The API key selection expression for the API"
value = aws_apigatewayv2_api.example.api_key_selection_expression
}

output "primary_network_interface_id" {
description = "List of IDs of the primary network interface of instances"
value = aws_instance.ec2[*].primary_network_interface_id
output "cors_configuration" {
description = "The CORS configuration for the API"
value = aws_apigatewayv2_api.example.cors_configuration
}

output "private_dns" {
description = "List of private DNS names assigned to the instances. Can only be used inside the Amazon EC2, and only available if you've enabled DNS hostnames for your VPC"
value = aws_instance.ec2[*].private_dns
}

output "private_ip" {
description = "List of private IP addresses assigned to the instances"
value = aws_instance.ec2[*].private_ip
}

output "security_groups" {
description = "List of associated security groups of instances"
value = aws_instance.ec2[*].security_groups
}

output "vpc_security_group_ids" {
description = "List of associated security groups of instances, if running in non-default VPC"
value = aws_instance.ec2[*].vpc_security_group_ids
}

output "subnet_id" {
description = "List of IDs of VPC subnets of instances"
value = aws_instance.ec2[*].subnet_id
output "tags_all" {
description = "Map of tags assigned to the resource"
value = aws_apigatewayv2_api.example.tags_all
}
Loading

0 comments on commit e0d1143

Please sign in to comment.