Amazon ElastiCache service supports Redis and Memcached. If you want in an in-memory caching solution for your application, check out the AWS-Docs. In this repository I cover two use cases.
Use-Case 1: Create an Amazon ElastiCache for Redis cluster using Terraform, and
Use-Case 2: Create an Amazon ElastiCache for Redis cluster and Amazon EC2 instances to access the cluster using Terraform.
If you are interested in Use-case 1, please refer to the create-amazon-elasticache branch.
For Use-case 2, this repository has the Terraform code to provision an Amazon ElastiCache for Redis cluster and all the supporting infrastructure components like Amazon VPC, subnets, security group, AWS KMS key, and AWS Secrets Manager secret. It also has addition AWS cloud resources like:
- an internet gateway and update the path in the route table attached to the public subnet
- an IAM instance profile and attach an IAM role with the two existing IAM policies to read from the SSM parameter store and AWS Secrets manager. These resources have the ElastiCache endpoint and auth_token stored that was created in Use-case 1.
- two Amazon EC2 instances in the public subnet with separate user data scripts to install Python libraries and create Python files inside the instances.
The process of provisioning is automated using GitHub Actions.
I discussed the concept in detail in my notes at -Connect to an Amazon ElastiCache cluster from an Amazon EC2 instance using Python.
I used Bridgecrew Checkov to scan the Terraform code for security vulnerabilities. Here is a link if you are interested in adding code scanning capabilities to your GitHub Actions pipeline -automate-terraform-configuration-scan-with-checkov-and-github-actions.
I also used Infracost to generate a cost estimate of building the architecture. If you want to learn more about adding Infracost estimates to your repository, head over to this note -estimate AWS Cloud resource cost with Infracost, Terraform, and GitHub Actions.
Lastly, I also automated the process of provisioning the resources using GitHub Actions pipeline and I discussed that in detail at -CI-CD with Terraform and GitHub Actions to deploy to AWS.
For this code to function without errors, I created an OpenID connect identity provider in Amazon Identity and Access Management that has a trust relationship with this GitHub repository. You can read about it here to get a detailed explanation with steps.
I stored the ARN of the IAM Role as a GitHub secret which is referred in the terraform.yml
file.
Since I used Infracost in this repository, I stored the INFRACOST_API_KEY
as a repository secret. It is referenced in the terraform.yml
GitHub actions workflow file.
As part of the Infracost integration, I also created a INFRACOST_API_KEY
and stored that as a GitHub Actions secret. I also managed the cost estimate process using a GitHub Actions variable INFRACOST_SCAN_TYPE
where the value is either hcl_code
or tf_plan
, depending on the type of scan desired.
Name | Version |
---|---|
aws | 5.20.1 |
random | 3.6.3 |
Name | Version |
---|---|
aws | 5.20.1 |
random | 3.6.3 |
Name | Source | Version |
---|---|---|
vpc | github.com/kunduso/terraform-aws-vpc | v1.0.1 |
Name | Description | Type | Default | Required |
---|---|---|---|---|
access_key | The access_key that belongs to the IAM user. | string |
"" |
no |
ami_name | The ami name of the image from where the instances will be created | list(string) |
[ |
no |
instance_type | The instance type of the EC2 instances | string |
"t3.medium" |
no |
name | The name of the application. | string |
"app-4" |
no |
region | AWS Cloud infrastructure region. | string |
"us-east-2" |
no |
secret_key | The secret_key that belongs to the IAM user. | string |
"" |
no |
subnet_cidr_private | CIDR blocks for the private subnets. | list(any) |
[ |
no |
subnet_cidr_public | CIDR blocks for the public subnets. | list(any) |
[ |
no |
vpc_cidr | CIDR for the VPC. | string |
"10.20.32.0/25" |
no |
No outputs.
Ensure that the policy attached to the IAM role whose credentials are being used in this configuration has permission to create and manage all the resources that are included in this repository.
Review the code including the terraform.yml
to understand the steps in the GitHub Actions pipeline. Also review the terraform code to understand all the concepts associated with creating an AWS VPC, subnets, internet gateway, route table, and route table association.
If you want to check the pipeline logs, click on the Build Badge (terrform-infra-provisioning) above the image in this ReadMe.
If you find any issues or have suggestions for improvement, feel free to open an issue or submit a pull request. Contributions are always welcome!
This code is released under the Unlincse License. See LICENSE.