我目前正在从事一个项目,希望使用 Terraform 自动执行两步流程。任务是:
从我目前进行的研究来看,这些任务似乎可以单独自动化。但是,我的目标是将其简化为一个自动化流程。有没有人完成过类似的事情或者可以就如何进行提供一些指导?
任何建议或指示将不胜感激。提前致谢!
在尝试解决这个问题时,我在 Terraform 中尝试了 AWS 和 Newrelic 提供程序。我的目标是弥合通过基础设施自动化部署的服务与 Newrelic 提供商之间的差距,最终目标是创建一个仪表板。不幸的是,我一直无法找到将这两个方面无缝集成的方法。 我正在寻找社区帮助,想出一个步骤来实施基础设施和 NR 仪表板 ...
下面是测试相关的terraform代码 main.tf(测试应用)
resource "aws_security_group" "alb_SG" {
name = "simple_app_SG"
description = "to test out NR integration"
vpc_id = "vpc-0f421b1b6e6xxxxx"
ingress {
description = "HTTP from internet"
from_port = 80
to_port = 80
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"]
}
tags = {
Name = "simple_app_SG-deployed"
}
}
resource "aws_security_group" "asg_SG" {
name = "ASG_SG"
description = "to test out NR integration"
vpc_id = "vpc-0f421b1b6e63xxxxxx"
ingress {
description = "HTTP from internet"
from_port = 80
to_port = 80
protocol = "tcp"
security_groups = [aws_security_group.alb_SG.id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "ASG_SG-deployed"
}
}
data "aws_ami" "amzn2" {
most_recent = true
owners = ["XXXXXXXXX"]
filter {
name = "name"
values = ["amzn2-prod"]
}
}
resource "aws_launch_template" "simpleapplt" {
name = "simpleapp-lt"
image_id = data.aws_ami.amzn2.id
instance_type = "t2.micro"
iam_instance_profile {
name = "xxxSSMCore"
}
network_interfaces {
device_index = 0
security_groups = [aws_security_group.asg_SG.id]
}
tag_specifications {
resource_type = "instance"
tags = {
Name = "simpleapp-lt-deployed by Methz"
}
}
user_data = base64encode("${var.ec2_user_data}}")
}
resource "aws_autoscaling_group" "simpleapp_asg" {
desired_capacity = 2
max_size = 3
min_size = 2
target_group_arns = [aws_lb_target_group.simpleappTG.arn]
launch_template {
id = aws_launch_template.simpleapplt.id
version = "$Latest"
}
vpc_zone_identifier = ["subnet-034c82ede2xxxxx", "subnet-08645c8945xxxxx"]
}
resource "aws_lb" "simpleappalb" {
name = "simpleappalb"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.alb_SG.id]
subnets = ["subnet-034c82edxxxxx", "subnet-08645c8945axxxxx"]
enable_deletion_protection = false
tags = {
Name = "simpleappalb-deployed "
}
}
resource "aws_lb_target_group" "simpleappTG" {
name = "simpleapp-tg"
port = 80
protocol = "HTTP"
vpc_id = "vpc-0f421b1b6e6xxxxx"
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 3
interval = 30
path = "/"
port = "traffic-port"
protocol = "HTTP"
}
}
resource "aws_lb_listener" "simpleappalb_listener" {
load_balancer_arn = aws_lb.simpleappalb.arn
port = 80
protocol = "HTTP"
default_action {
target_group_arn = aws_lb_target_group.simpleappTG.arn
type = "forward"
}
tags = {
Name = "simpleappalb_listener-deployed"
}
}
providers.tf(用于测试应用程序)
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.66.1"
}
newrelic = {
source = "newrelic/newrelic"
version = "3.22.0"
}
}
}
provider "aws" {
# Configuration options
region = "us-east-1"
}
# Configure the New Relic provider
provider "newrelic" {
account_id = XXXXXX
api_key = "NRAK-XXXXXXXXXXXXXXXXX" # Usually prefixed with 'NRAK'
region = "US" # Valid regions are US and EU
}
变量.tf
variable "ec2_user_data" {
description = "user data for ec2 instance"
type = string
default = <<EOF
#!/bin/bash
sudo systemctl start amazon-ssm-agent
# Install Apache on Ubuntu
sudo apt update -y
sudo apt install -y apache2
sudo cat > /var/www/html/index.html << EOF
<html>
<head>
<title> Apache on Ubuntu </title>
</head>
<body>
<p> Apache was installed using Terraform!
</body>
</html>
EOF
}
以下包含我在 terraform 中使用 newrelic 提供程序完成的测试代码。
# This defines the values we're expecting per widget - see terraform.tfvars for the actual configured values
variable "config" {
type = list(object({
name = string
domain = string
}))
}
# This generates the 'rows' of widgets from the CONFIG object
data "template_file" "widgets" {
template = templatefile(
"${path.module}/composed_widgets.json.tftpl",
{
ACCOUNTID = xxxxxx
CONFIG = var.config
}
)
}
resource "newrelic_one_dashboard_json" "composed_dashboard" {
json = data.template_file.widgets.rendered
}
#Lets tag terraform managed dashboards!
resource "newrelic_entity_tags" "composed_dashboard" {
guid = newrelic_one_dashboard_json.composed_dashboard.guid
tag {
key = "terraform"
values = [true]
}
}
output "composed_dashboard" {
value=newrelic_one_dashboard_json.composed_dashboard.permalink
}
json.tftl 文件用于 NR 部署,这来自 terraform 中提供的示例
{
"name": "SRE POD8 - MLM / HE Portal PPE - TF - Dynamic",
"description": null,
"permissions": "PUBLIC_READ_ONLY",
"pages": [
{
"name": "JSON Composed Example",
"description": null,
"widgets": [
%{~ for index, api in CONFIG ~}
%{ if index!=0 },
%{ endif }
{
"title": "",
"layout": {
"column": 4,
"row": ${(index * 4) + 2 },
"width": 1,
"height": 3
},
"linkedEntityGuids": null,
"visualization": {
"id": "viz.billboard"
},
"rawConfiguration": {
"facet": {
"showOtherSeries": false
},
"nrqlQueries": [
{
"accountId": ${ACCOUNTID},
"query": "SELECT average(duration) as 'Avg Duration',percentile(duration,95) as 'p95 Duration' from Public_APICall where api='${api.domain}'"
}
],
"platformOptions": {
"ignoreTimeRange": true
},
"thresholds": [
{
"alertSeverity": "CRITICAL",
"value": 0.3
}
]
}
},
{
"title": "",
"layout": {
"column": 9,
"row": ${(index * 4) + 2 },
"width": 4,
"height": 3
},
"linkedEntityGuids": null,
"visualization": {
"id": "viz.stacked-bar"
},
"rawConfiguration": {
"facet": {
"showOtherSeries": false
},
"legend": {
"enabled": false
},
"nrqlQueries": [
{
"accountId": ${ACCOUNTID},
"query": "SELECT count(*) as 'Call breakdown' from Public_APICall where api='${api.domain}' facet http.url timeseries 6 hour since 1 week ago limit 25"
}
],
"platformOptions": {
"ignoreTimeRange": false
}
}
}
%{ endfor ~}
]
}
]
}