Skip to content

Commit

Permalink
feat: deploy CMP control center on AWS using terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
Kris20030907 committed Aug 7, 2024
1 parent 59c84d2 commit ca21b86
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
46 changes: 46 additions & 0 deletions modules/aws-cn-module/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## Quickly start the cmp service through terraform

### Environmental preparation

1. Create a VPC that meets the conditions
Reference documentation:[VPC Create](https://docs.automq.com/zh/automq-cloud/getting-started/create-byoc-environment/aws/step-1-installing-env-with-ami)
You need to get the VPC_ID

2. Parameter configuration

You need to modify the contents of the file `terraform.tfvars` to ensure correct deployment of cmp. The parameters you need to modify are:
```bash
aws_region = "cn-northwest-1"
aws_access_key = "AKIAXXXXXXR4SC45" # AWS Access Key
aws_secret_key = "7smvBw1XXXXGMPXxx9FI/LXSnXXXX7W" # AWS Secret Key
aws_vpc_id = "vpc-0XXXXc043ae" # Fill in the VPC ID created in the previous step.
aws_ami_id = "ami-035193f2cdb529fda" # Default is OK
```
> The version of cmp ami can default to the provided version.
### terraform deployment

Execute the command in the directory `/terraform-provider-automq/modules/aws-cn-module`:

```bash
terraform init

terraform plan

terraform apply -auto-approve
```

After successful deployment, some prompt information will be output, such as:

Please wait for the service to initialize, about 1 min. Once ready, you can access the service at http://${aws_eip.web_ip.public_ip}:8080

### cmp initialization

Here you can refer to the official website documentation to complete the initialization:[Init CMP](https://docs.automq.com/zh/automq-cloud/getting-started/create-byoc-environment/aws/step-2-initializing-the-environment)

### Release resources

Execute the command in the directory `/terraform-provider-automq/modules/aws-cn-module`:
```bash
terraform destroy -auto-approve
```
66 changes: 66 additions & 0 deletions modules/aws-cn-module/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# main.tf

provider "aws" {
region = var.aws_region
access_key = var.aws_access_key
secret_key = var.aws_secret_key
}

data "aws_vpc" "selected" {
id = var.aws_vpc_id
}

data "aws_subnets" "all" {
filter {
name = "vpc-id"
values = [data.aws_vpc.selected.id]
}
}

data "aws_subnet" "first" {
id = data.aws_subnets.all.ids[0]
}

resource "aws_security_group" "allow_all" {
vpc_id = data.aws_vpc.selected.id

ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}

resource "aws_instance" "web" {
ami = var.aws_ami_id
instance_type = "c5.xlarge"
subnet_id = data.aws_subnet.first.id
vpc_security_group_ids = [aws_security_group.allow_all.id]

root_block_device {
volume_size = 10
volume_type = "gp2"
}

ebs_block_device {
device_name = "/dev/sdh"
volume_size = 20
volume_type = "gp3"
}

tags = {
Name = "cmp-service"
}
}

resource "aws_eip" "web_ip" {
instance = aws_instance.web.id
}
21 changes: 21 additions & 0 deletions modules/aws-cn-module/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# outputs.tf

output "instance_ip" {
value = aws_eip.web_ip.public_ip
}

output "vpc_id" {
value = data.aws_vpc.selected.id
}

output "ebs_volume_id" {
value = [for bd in aws_instance.web.ebs_block_device : bd.volume_id][0]
}

output "security_group_name" {
value = aws_security_group.allow_all.name
}

output "access_message" {
value = "Please wait for the service to initialize, about 1 min. Once ready, you can access the service at http://${aws_eip.web_ip.public_ip}:8080"
}
29 changes: 29 additions & 0 deletions modules/aws-cn-module/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# variables.tf

variable "aws_region" {
description = "The AWS region to deploy in"
type = string
default = "cn-northwest-1"
}

variable "aws_access_key" {
description = "The AWS access key"
type = string
sensitive = true
}

variable "aws_secret_key" {
description = "The AWS secret key"
type = string
sensitive = true
}

variable "aws_vpc_id" {
description = "The ID of the VPC"
type = string
}

variable "aws_ami_id" {
description = "The ID of the AMI to use"
type = string
}

0 comments on commit ca21b86

Please sign in to comment.