IAC:
Terraform:
Terraform Module:
This repository contains Terraform modules that can be used across different projects. These modules are designed to provide generic infrastructure components for easy reuse.
Terraform uses AWS credentials to authenticate and interact with your AWS environment. Follow the steps below to set up AWS authentication for Terraform:
Dependencies:
1. AWS CLI Configuration:
-
Install the AWS CLI by following the instructions in the AWS CLI User Guide.
-
Run
aws configureto set up your AWS credentials. You will be prompted to enter your AWS Access Key ID, Secret Access Key, default region, and output format.
2. Using Personal Access Token (PAT):
- To Generate a Personal Access Token Go to your GitHub account settings:
- Navigate into "Developer settings" > "Personal access tokens".
- Click on "Generate token" and provide the necessary scopes, such as read:packages and repo.
- Copy the generated token.
- Use the Personal Access Token in Terraform:
- In your Terraform configuration, modify the source attribute in the module block to include the PAT.
3. Terraform Configuration
-
Ensure that Terraform is installed on your machine. Follow the instructions on the Terraform website for installation.
-
Create Terraform configuration files (e.g.,
provider.tf), you don't need to hardcode AWS credentials. Instead, use AWS CLI configuration or environment variable and terraform provider.terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.30.0"
}
}
}
provider "aws" {
region = "<aws region>" # Replace with your desired AWS region
}
-
To use the module you can create (e.g.,
main.tf) in your project and reference the module:module "example" {
source = "<path-to-cloned-repo>/terraform-modules/<module-name>"
# Customize module variables as needed
variable_name = "value"
} -
Initialize the Terraform configuration:
terraform init -
Apply the configuration:
terraform apply
[!NOTE]
Please remember to put Personal Access Token (PAT) on source accordingly to lamda and vpc module.
- For Example:-
source = "git::https://<PAT>@github.com/ChainSafe/infrastructure-general.git//terraform/modules/lambda?ref=generic-tf-infra"
Example to Use Lambda Module:
This module will create IAM role and lambda function. And to use this module you need to create a terraform configuration file (e.g., lambda.tf) in your project with the reference of lambda module:
provider "archive" {}
data "archive_file" "zip" {
type = "zip"
source_file = "<path of your code>"
output_path = "<path of you output zip code>"
}
module "lambda_module" {
source = "git::https://<PAT>@github.com/ChainSafe/infrastructure-general.git//terraform/modules/lambda?ref=generic-tf-infra"
lambda_role_name = "<iam role name>"
lambda_function_name = "<function name>
lambda_filename = "${data.archive_file.zip.output_path}"
lambda_source_code_hash = "${data.archive_file.zip.output_base64sha256}"
lambda_handler = "<lambda handler name>
lambda_runtime = "<labmda runtime>"
private_subnet_id = module.vpc_module.private_subnet_id
architectures = ["x86_64"]
package_type = "Zip"
memory_size = 128
vpc_id = module.vpc_module.vpc_id
security_group_name = "sg_east1"
ingress_rules = ["80"]
ingress_cidr_blocks = ["10.0.0.0/16"]
ingress_rules_from_port = ["80"]
ingress_rules_to_port = ["80"]
ingress_rules_protocols = ["tcp"]
egress_rules = [0]
egress_cidr_blocks = ["0.0.0.0/0"]
egress_rules_from_port = [0]
egress_rules_to_port = [0]
egress_rules_protocols = [-1]
}
Example to Use VPC Module
This module will create a VPC, Subnet, ElasticIP, Internet Gateway, Nat Gateway and Route table. And to use this module you need to create a terraform configuration file (e.g., vpc.tf) in your project with the reference of VPC module:
module "vpc_module" {
source = "git::https://<PAT>@github.com/ChainSafe/infrastructure-general.git//terraform/modules/vpc?ref=generic-tf-infra"
vpc_cidr = ""
vpc_name = ""
public_subnets = [
{
name = "", #public subnet name
cidr = "", #public subnet cidr
availability_zones = "", #public subnet availability zones
},
{
name = "",
cidr = "",
availability_zones = ""
}
# Add more subnets as needed
]
private_subnets = [
{
name = "", #private subnet name
cidr = "", #private subnet cidr
availability_zones = "" #private subnet availability zones
},
{
name = "",
cidr = "",
availability_zones = ""
}
# Add more subnets as needed
]
internet_gateway_name = ""
nat_gateway_name = ""
private_route_table_name = ""
public_route_table_name = ""
}