本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
了解如何使用. AWS Proton地形
AWS Proton 支援 Terraform IAC 的自我管理佈建。
有關響應提取請求並實現基礎結構佈建的佈建存儲庫的完整示例,請參閱上的 Terraform OpenSource GitHub 動作自動化模板
自我管理佈建如何與 Terraform IaC 範本套件組合檔案搭配使用:
AWS Proton 與地形 IaC 的考慮因素:
-
AWS Proton 不管理您的 Terraform 配置。
-
您必須使用註冊啟動設定存放庫 AWS Proton。 AWS Proton 在此儲存庫上提出提取要求。
-
您必須建立 CodeStar 連線才能 AWS Proton 與佈建存放庫連線。
-
若要從 AWS Proton 編譯的 IaC 檔案進行佈建,您必須回應提 AWS Proton 取要求。 AWS Proton 在環境和服務建立和更新動作之後發出提取要求。如需詳細資訊,請參閱 AWS Proton 環境 及 AWS Proton 服務。
-
若要從 AWS Proton 編譯的 IaC 檔案佈建管線,您必須建立 CI/CD 管線存放庫。
-
以提取要求為基礎 AWS Proton 的佈建自動化必須包含通知任何已佈建 AWS Proton 資源狀態變更的步驟。您可以使用 AWS Proton NotifyResourceDeploymentStatusChange API。
-
您無法將從 IaC 檔案建立的服務、管線和元件部署到從 Terraform CloudFormation IaC 檔案建立的環境中。
-
您無法將從 Terraform IaC 檔案建立的服務、管線和元件部署到從 IaC 檔案建立的環境中 CloudFormation 。
準備您的 Terraform IaC 檔案時 AWS Proton,您會將命名空間附加至輸入變數,如下列範例所示。如需詳細資訊,請參閱參數。
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
// This tells terraform to store the state file in s3 at the location
// s3://terraform-state-bucket/tf-os-sample/terraform.tfstate
backend "s3" {
bucket = "terraform-state-bucket"
key = "tf-os-sample/terraform.tfstate"
region = "us-east-1"
}
}
// Configure the AWS Provider
provider "aws" {
region = "us-east-1"
default_tags {
tags = var.proton_tags
}
}
resource "aws_ssm_parameter" "my_ssm_parameter" {
name = "my_ssm_parameter"
type = "String"
// Use the Proton environment.inputs. namespace
value = var.environment.inputs.ssm_parameter_value
}
編譯基礎架構即程式
當您建立環境或服務時,會使用主控台或spec file
輸入將基礎結構 AWS Proton 編譯為程式碼檔案。它會為您的輸入創建proton.
和resource-type
.variables.tfproton.auto.tfvars.json
文件,可以通過 Terraform 中使用,如下面的例子。這些檔案位於與環境或服務執行個體名稱相符的資料夾中的指定儲存庫中。
此範例顯示如何在變數定義和變數值中 AWS Proton 包含標籤,以及如何將這些 AWS Proton 標籤傳播至已佈建的資源。如需詳細資訊,請參閱 標籤傳播至已佈建資源。
開發/環境 .tf:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
// This tells terraform to store the state file in s3 at the location
// s3://terraform-state-bucket/tf-os-sample/terraform.tfstate
backend "s3" {
bucket = "terraform-state-bucket"
key = "tf-os-sample/terraform.tfstate"
region = "us-east-1"
}
}
// Configure the AWS Provider
provider "aws" {
region = "us-east-1"
default_tags {
tags = var.proton_tags
}
}
resource "aws_ssm_parameter" "my_ssm_parameter" {
name = "my_ssm_parameter"
type = "String"
// Use the Proton environment.inputs. namespace
value = var.environment.inputs.ssm_parameter_value
}
開發/質子環境變量 .tf:
variable "environment" {
type = object({
inputs = map(string)
name = string
})
}
variable "proton_tags" {
type = map(string)
default = null
}
開發/質子. 自動 .tfvar.json:
{
"environment": {
"name": "dev",
"inputs": {
"ssm_parameter_value": "MyNewParamValue"
}
}
"proton_tags" : {
"proton:account" : "123456789012",
"proton:template" : "arn:aws:proton:us-east-1:123456789012:environment-template/fargate-env",
"proton:environment" : "arn:aws:proton:us-east-1:123456789012:environment/dev"
}
}
儲存庫路徑
AWS Proton 使用來自環境或服務的控制台或規格輸入創建操作來查找存儲庫和路徑,它是找到編譯的 IaC 文件。輸入值會傳遞至命名空間的輸入參數。
AWS Proton 支援兩種儲存庫路徑配置。在下列範例中,路徑是由來自兩個環境的命名空間資源參數來命名。每個環境都有兩個服務的服務執行個體,而其中一個服務的服務執行個體已直接定義元件。
資源類型 | 名稱參數 | = | 資源名稱 |
---|---|---|---|
Environment | environment.name |
= | "env-prod" |
Environment | environment.name |
"env-staged" |
|
服務 | service.name |
"service-one" |
|
服務執行個體 | service_instance.name |
"instance-one-prod" |
|
服務執行個體 | service_instance.name |
"instance-one-staged" |
|
服務 | service.name |
"service-two" |
|
服務執行個體 | service_instance.name |
"instance-two-prod" |
|
元件 | service_instance.components.default.name |
"component-prod" |
|
服務執行個體 | service_instance.name |
"instance-two-staged" |
|
元件 | service_instance.components.default.name |
"component-staged" |
如果 AWS Proton 找到含有environments
資料夾的指定儲存庫,則會建立一個資料夾,其中包含已編譯的 IaC 檔案,並以. environment.name
如果 AWS Proton 找到指定的儲存庫,其中包含與服務執行個體相容環境名稱相容的資料environments
夾名稱相符的資料夾,則會建立包含已編譯執行個體 IaC 檔案的資料夾,並以. service_instance.name
/repo
/environments
/env-prod # environment folder
main.tf
proton.environment.variables.tf
proton.auto.tfvars.json
/service-one-instance-one-prod # instance folder
main.tf
proton.service_instance.variables.tf
proton.auto.tfvars.json
/service-two-instance-two-prod # instance folder
main.tf
proton.service_instance.variables.tf
proton.auto.tfvars.json
/component-prod # component folder
main.tf
proton.component.variables.tf
proton.auto.tfvars.json
/env-staged # environment folder
main.tf
proton.variables.tf
proton.auto.tfvars.json
/service-one-instance-one-staged # instance folder
main.tf
proton.service_instance.variables.tf
proton.auto.tfvars.json
/service-two-instance-two-staged # instance folder
main.tf
proton.service_instance.variables.tf
proton.auto.tfvars.json
/component-staged # component folder
main.tf
proton.component.variables.tf
proton.auto.tfvars.json