Skip to content

Commit

Permalink
Merge pull request #25 from aws-ia/reorg-example-fixtures
Browse files Browse the repository at this point in the history
move sample app and refactor tests
  • Loading branch information
drewmullen authored Jul 1, 2022
2 parents 4bb5ca7 + cfc19e4 commit 308883e
Show file tree
Hide file tree
Showing 38 changed files with 223 additions and 114 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ override.tf.json

.archive_files
results.json

go.mod
go.sum
6 changes: 5 additions & 1 deletion .tflint.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@

plugin "aws" {
enabled = true
version = "0.12.0"
version = "0.14.0"
source = "github.com/terraform-linters/tflint-ruleset-aws"
}

config {
module = true
force = false
ignore_module = {
"test/hcl_fixtures/sample_app" = true
"test/hcl_fixtures/sample_app/modules/app" = true
}
}

rule "terraform_required_providers" {
Expand Down
4 changes: 3 additions & 1 deletion examples/basic/.header.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Sample App Deployment with Route53 Application Recovery Controller

To deploy a sample app consisting of Auto-Scaling Groups, ALBs, and DynamoDB Global table. App deployment has been adapted from [this blog post](https://aws.amazon.com/blogs/networking-and-content-delivery/running-recovery-oriented-applications-with-amazon-route-53-application-recovery-controller-aws-ci-cd-tools-and-terraform/).
This is as example of adding ARC support to an app deployed using Regions as its Cell boundaries. Each Cell (Region) contains a replica of the application stack which includes an Application Load Balancer (ALB), EC2 Auto-Scaling Group (ASG), and DynamoDB.

The `main.tf` contains the application stack HCL but it is commented out because it is *only an example*. The sample app design was adapted from [this blog post](https://aws.amazon.com/blogs/networking-and-content-delivery/running-recovery-oriented-applications-with-amazon-route-53-application-recovery-controller-aws-ci-cd-tools-and-terraform/).

```bash
terraform init
Expand Down
18 changes: 0 additions & 18 deletions examples/basic/.terraform.lock.hcl

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

9 changes: 7 additions & 2 deletions examples/basic/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<!-- BEGIN_TF_DOCS -->
# Sample App Deployment with Route53 Application Recovery Controller

To deploy a sample app consisting of Auto-Scaling Groups, ALBs, and DynamoDB Global table. App deployment has been adapted from [this blog post](https://aws.amazon.com/blogs/networking-and-content-delivery/running-recovery-oriented-applications-with-amazon-route-53-application-recovery-controller-aws-ci-cd-tools-and-terraform/).
This is as example of adding ARC support to an app deployed using Regions as its Cell boundaries. Each Cell (Region) contains a replica of the application stack which includes an Application Load Balancer (ALB), EC2 Auto-Scaling Group (ASG), and DynamoDB.

The `main.tf` contains the application stack HCL but it is commented out because it is *only an example*. The sample app design was adapted from [this blog post](https://aws.amazon.com/blogs/networking-and-content-delivery/running-recovery-oriented-applications-with-amazon-route-53-application-recovery-controller-aws-ci-cd-tools-and-terraform/).

```bash
terraform init
Expand All @@ -13,6 +15,7 @@ terraform apply -var 'allowed_ips=["1.2.3.4/32"]'

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.15.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.68 |

## Providers
Expand All @@ -24,7 +27,6 @@ No providers.
| Name | Source | Version |
|------|--------|---------|
| <a name="module_basic_recovery_controller_example"></a> [basic\_recovery\_controller\_example](#module\_basic\_recovery\_controller\_example) | ../.. | n/a |
| <a name="module_sample_app"></a> [sample\_app](#module\_sample\_app) | ../../modules/sample_app | n/a |

## Resources

Expand All @@ -35,6 +37,9 @@ No resources.
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_allowed_ips"></a> [allowed\_ips](#input\_allowed\_ips) | List of cidrs to allow communication to your app. | `list(string)` | n/a | yes |
| <a name="input_alternative_app_arns"></a> [alternative\_app\_arns](#input\_alternative\_app\_arns) | Outputs passed to module for testing purposes (/test/example\_basic\_test.go) | `any` | n/a | yes |
| <a name="input_dynamodb_table_arn"></a> [dynamodb\_table\_arn](#input\_dynamodb\_table\_arn) | (optional) describe your variable | `string` | n/a | yes |
| <a name="input_primary_app_arns"></a> [primary\_app\_arns](#input\_primary\_app\_arns) | Outputs passed to module for testing purposes (/test/example\_basic\_test.go) | `any` | n/a | yes |
| <a name="input_cells_definition"></a> [cells\_definition](#input\_cells\_definition) | Definition of the resources that makeup your Cell that you want monitored by ARC. | `map(map(string))` | `null` | no |
| <a name="input_create_recovery_cluster"></a> [create\_recovery\_cluster](#input\_create\_recovery\_cluster) | Create the Routing Control Cluster and associated resources. | `bool` | `false` | no |
| <a name="input_hosted_zone"></a> [hosted\_zone](#input\_hosted\_zone) | Info about the hosted zone. If the `name` or `zone_id` is not passed, a search will be performed using the values provided. Leave null to not create Route53 Alias records (required for LB functionality) . | <pre>object({<br> name = optional(string)<br> private_zone = optional(bool)<br> vpc_id = optional(number)<br> tags = optional(map(string))<br> zone_id = optional(string)<br> })</pre> | <pre>{<br> "name": null,<br> "zone_id": null<br>}</pre> | no |
Expand Down
20 changes: 10 additions & 10 deletions examples/basic/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ module "basic_recovery_controller_example" {

cells_definition = {
us-east-1 = {
autoscaling = module.sample_app.asg_primary.arn
elasticloadbalancing = module.sample_app.alb_primary.arn
dynamodb = module.sample_app.dynamodb.arn
autoscaling = var.primary_app_arns["autoscaling"]
elasticloadbalancing = var.primary_app_arns["elasticloadbalancing"]
dynamodb = var.dynamodb_table_arn
}
us-west-2 = {
autoscaling = module.sample_app.asg_alternative.arn
elasticloadbalancing = module.sample_app.alb_alternative.arn
dynamodb = replace(module.sample_app.dynamodb.arn, "us-east-1", "us-west-2")
autoscaling = var.alternative_app_arns["autoscaling"]
elasticloadbalancing = var.alternative_app_arns["elasticloadbalancing"]
dynamodb = replace(var.dynamodb_table_arn, "us-east-1", "us-west-2")
}
}
}

# sample app is for demonstratino purposes only and is not safe for production workloads
module "sample_app" {
source = "../../modules/sample_app"
allowed_ips = var.allowed_ips
}
# module "sample_app" {
# source = "../../test/hcl_fixtures/sample_app"
# allowed_ips = var.allowed_ips
# }

# Regions must be hard coded for example since theyre defined explicitly in `cells_definition`
provider "aws" {
Expand Down
3 changes: 2 additions & 1 deletion examples/basic/versions.tf → examples/basic/providers.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
terraform {
experiments = [module_variable_optional_attrs]
required_version = ">= 0.15.0"
experiments = [module_variable_optional_attrs]

required_providers {
aws = {
Expand Down
15 changes: 15 additions & 0 deletions examples/basic/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,18 @@ variable "allowed_ips" {
description = "List of cidrs to allow communication to your app."
type = list(string)
}

variable "primary_app_arns" {
type = any
description = "Outputs passed to module for testing purposes (/test/example_basic_test.go)"
}

variable "alternative_app_arns" {
type = any
description = "Outputs passed to module for testing purposes (/test/example_basic_test.go)"
}

variable "dynamodb_table_arn" {
type = string
description = "(optional) describe your variable"
}
11 changes: 0 additions & 11 deletions modules/sample_app/modules/app/outputs.tf

This file was deleted.

37 changes: 0 additions & 37 deletions modules/sample_app/outputs.tf

This file was deleted.

File renamed without changes.
26 changes: 25 additions & 1 deletion test/examples_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,34 @@ import (

func TestExamplesBasic(t *testing.T) {

sampleApp := &terraform.Options{
TerraformDir: "./hcl_fixtures/sample_app",
Vars: map[string]interface{}{
"allowed_ips": []string{"127.0.0.1/32"},
},
}

defer terraform.Destroy(t, sampleApp)
terraform.InitAndApply(t, sampleApp)

primary_asg_arn := terraform.Output(t, sampleApp, "asg_primary")
primary_alb_arn := terraform.Output(t, sampleApp, "alb_primary")
alternative_asg_arn := terraform.Output(t, sampleApp, "asg_alternative")
alternative_alb_arn := terraform.Output(t, sampleApp, "alb_alternative")
dynamodb := terraform.Output(t, sampleApp, "dynamodb_arn")

terraformOptions := &terraform.Options{
TerraformDir: "../examples/basic",
Vars: map[string]interface{}{
"allowed_ips": []string{"127.0.0.1/32"},
"dynamodb_table_arn": dynamodb,
"primary_app_arns": map[string]string{
"autoscaling": primary_asg_arn,
"elasticloadbalancing": primary_alb_arn,
},
"alternative_app_arns": map[string]string{
"autoscaling": alternative_asg_arn,
"elasticloadbalancing": alternative_alb_arn,
},
},
}

Expand Down
File renamed without changes.
40 changes: 40 additions & 0 deletions test/hcl_fixtures/sample_app/.terraform.lock.hcl

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

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.15.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.68 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_archive"></a> [archive](#provider\_archive) | n/a |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.68 |
| <a name="provider_aws.alternative"></a> [aws.alternative](#provider\_aws.alternative) | >= 3.68 |

Expand Down Expand Up @@ -46,8 +46,6 @@
| [aws_s3_bucket_public_access_block.app_source_code](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
| [aws_s3_bucket_public_access_block.s3_region_1_public_block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
| [aws_s3_bucket_public_access_block.s3_region_2_public_block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
| [archive_file.app_source_code](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source |
| [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
Expand All @@ -56,20 +54,19 @@
|------|-------------|------|---------|:--------:|
| <a name="input_allowed_ips"></a> [allowed\_ips](#input\_allowed\_ips) | List of cidrs to allow communication to your app. | `list(string)` | n/a | yes |
| <a name="input_alternative_region"></a> [alternative\_region](#input\_alternative\_region) | The Alternative AWS region to deploy app to. | `string` | `"us-west-2"` | no |
| <a name="input_app_name"></a> [app\_name](#input\_app\_name) | n/a | `string` | `"tic-tac-toe"` | no |
| <a name="input_app_name"></a> [app\_name](#input\_app\_name) | test app variable | `string` | `"tic-tac-toe"` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_alb_alternative"></a> [alb\_alternative](#output\_alb\_alternative) | n/a |
| <a name="output_alb_primary"></a> [alb\_primary](#output\_alb\_primary) | n/a |
| <a name="output_asg_alternative"></a> [asg\_alternative](#output\_asg\_alternative) | n/a |
| <a name="output_asg_primary"></a> [asg\_primary](#output\_asg\_primary) | n/a |
| <a name="output_code_deploy"></a> [code\_deploy](#output\_code\_deploy) | n/a |
| <a name="output_dynamodb"></a> [dynamodb](#output\_dynamodb) | n/a |
| <a name="output_dynamodb-arn"></a> [dynamodb-arn](#output\_dynamodb-arn) | n/a |
| <a name="output_pipeline_url"></a> [pipeline\_url](#output\_pipeline\_url) | n/a |
| <a name="output_s3_bucket_region_1"></a> [s3\_bucket\_region\_1](#output\_s3\_bucket\_region\_1) | n/a |
| <a name="output_s3_bucket_region_2"></a> [s3\_bucket\_region\_2](#output\_s3\_bucket\_region\_2) | n/a |
| <a name="output_alb_alternative"></a> [alb\_alternative](#output\_alb\_alternative) | fixture output |
| <a name="output_alb_primary"></a> [alb\_primary](#output\_alb\_primary) | fixture output |
| <a name="output_asg_alternative"></a> [asg\_alternative](#output\_asg\_alternative) | fixture output |
| <a name="output_asg_primary"></a> [asg\_primary](#output\_asg\_primary) | fixture output |
| <a name="output_code_deploy"></a> [code\_deploy](#output\_code\_deploy) | fixture output |
| <a name="output_dynamodb_arn"></a> [dynamodb\_arn](#output\_dynamodb\_arn) | fixture output |
| <a name="output_pipeline_url"></a> [pipeline\_url](#output\_pipeline\_url) | fixture output |
| <a name="output_s3_bucket_region_1"></a> [s3\_bucket\_region\_1](#output\_s3\_bucket\_region\_1) | fixture output |
| <a name="output_s3_bucket_region_2"></a> [s3\_bucket\_region\_2](#output\_s3\_bucket\_region\_2) | fixture output |
<!-- END_TF_DOCS -->
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,3 @@ resource "aws_codepipeline" "pipeline" {
}
}
}

output "pipeline_url" {
value = "https://console.aws.amazon.com/codepipeline/home?region=${data.aws_region.current.name}#/view/${aws_codepipeline.pipeline.id}"
}


Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}

#tfsec:ignore:aws-s3-enable-bucket-encryption tfsec:ignore:aws-s3-enable-bucket-logging
resource "aws_s3_bucket" "app_source_code" {
Expand All @@ -21,12 +20,6 @@ resource "aws_s3_bucket_public_access_block" "app_source_code" {
restrict_public_buckets = true
}

data "archive_file" "app_source_code" {
source_dir = "${path.module}/src/"
type = "zip"
output_path = "${path.root}/.archive_files/${var.app_name}.zip"
}

resource "aws_s3_bucket_object" "app_source_code" {
bucket = aws_s3_bucket.app_source_code.id
key = "${var.app_name}.zip"
Expand Down
22 changes: 22 additions & 0 deletions test/hcl_fixtures/sample_app/modules/app/.terraform.lock.hcl

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

File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 308883e

Please sign in to comment.