我正在尝试使用 terraform 创建一个 AWS lambda 函数。我的 terraform 目录看起来像
我的 lambda 函数存储在/terraform/lambda/files/lambda_function.py 中。
每当我应用 terraform 时,我都有一个“null_resource”,它会在本地机器上执行一些命令来压缩 python 文件
variable "pythonfile" {
description = "lambda function python filename"
type = "string"
}
resource "null_resource" "lambda_preconditions" {
triggers {
always_run = "${uuid()}"
}
provisioner "local-exec" {
command = "rm -rf ${path.module}/files/zips"
}
provisioner "local-exec" {
command = "mkdir -p ${path.module}/files/zips"
}
provisioner "local-exec" {
command = "cp -R ${path.module}/files/${var.pythonfile} ${path.module}/files/zips/lambda_function.py"
}
provisioner "local-exec" { …Run Code Online (Sandbox Code Playgroud) amazon-web-services aws-lambda terraform terraform-provider-aws
我RDS为我的开发团队编写了一个自定义模块,用于部署RDS实例。我正在使用BitBucket源代码控制,并且我正在尝试集成一个BitBucket管道以terraform validate在我的.tf文件上运行以验证语法,然后再将其提供给开发人员使用。terraform init运行良好,但是当我运行时terraform validate出现以下错误:Error: Missing required argument. The argument "region" is required, but was not set.查看文档后,我很困惑,如果该命令实际上没有部署任何内容,为什么会检查已声明的提供程序?诚然,我是编写模块的新手。也许这不是我想要完成的正确命令?
Terraform version: v0.12.7
AWS Provider version: 2.24
bitbucket-pipelines.yml:
image: hashicorp/terraform:full
pipelines:
branches:
master:
- step:
script:
- terraform version
- terraform init
- terraform validate
Run Code Online (Sandbox Code Playgroud)
Module tree:
??? CHANGELOG.md
??? README.md
??? bitbucket-pipelines.yml
??? main.tf
??? modules
? ??? db_instance
? ? ??? README.md
? ? …Run Code Online (Sandbox Code Playgroud) bitbucket continuous-deployment terraform terraform-provider-aws
我们将最新批准的 AMI 存储在 AWS 参数存储中。使用 Terraform 创建新实例时,我想以编程方式获取此 AMI ID。我有一个命令来提取 AMI ID,但我不确定如何将它与 Terraform 一起使用。
这是我用来提取 AMI ID 的命令:
$(aws ssm get-parameter --name /path/to/ami --query 'Parameter.Value' --output text)
Run Code Online (Sandbox Code Playgroud)
这是我的 Terraform 脚本:
resource "aws_instance" "nginx" {
ami = "ami-c58c1dd3" # pull value from parameter store
instance_type = "t2.micro"
#key_name = "${var.key_name}"
provisioner "remote-exec" {
inline = [
"sudo yum install nginx -y",
"sudo service nginx start"
]
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用命令在 Terraform 脚本中提取 AMI ID?
如何将现有 AWS 资源导入 Terraform 状态(该资源存在于不同账户中)?
terraform import module.mymodule.aws_iam_policy.policy arn:aws:iam::123456789012:policy/mypolicy
Run Code Online (Sandbox Code Playgroud)
给出以下错误:
Error: Cannot import non-existent remote object
While attempting to import an existing object to aws_iam_policy.policy, the
provider detected that no object exists with the given id. Only pre-existing
objects can be imported; check that the id is correct and that it is
associated with the provider's configured region or endpoint, or use
"terraform apply" to create a new remote object for this resource.
Run Code Online (Sandbox Code Playgroud)
该资源是使用在名为 的模块中定义的不同配置程序在一个帐户中创建的mymodule:
module "mymodule" { …Run Code Online (Sandbox Code Playgroud) 我正在使用 Terraform 来启动我的云环境。
似乎即使是很小的配置更改也会影响幕后的许多资源。
例如,在我创建 AWS 实例的情况下 - 一个小的更改将导致所有实例的自动生成:
-/+ aws_instance.DC (new resource required)
id: "i-075deb0aaa57c2d" => <computed> (forces new resource) <----- How can we avoid that?
ami: "ami-01e306baaaa0a6f65" => "ami-01e306baaaa0a6f65"
arn: "arn:aws:ec2:ap-southeast-2:857671114786:instance/i-075deb0aaa57c2d" => <computed>
associate_public_ip_address: "false" => <computed>
availability_zone: "ap-southeast-2a" => <computed>
.
.
Run Code Online (Sandbox Code Playgroud)
我的问题特别与作为提供者的 AWS 相关:
我们如何避免每次破坏/创建资源?
也许是 Terraform 中的相关标志?
相关主题:
Terraform > ipv6_address_count: "" => "0"(强制新资源)
编辑:
深入了解计划输出,其中一项资源似乎发生了变化:
security_groups.#: "0" => "1" (forces new resource)
security_groups.837544107: "" => "sg-0892062659392afa9" (forces new resource) …Run Code Online (Sandbox Code Playgroud) 我的任务定义:
resource "aws_ecs_task_definition" "datadog" {
family = "${var.environment}-datadog-agent-task"
task_role_arn = "arn:aws:iam::xxxxxxxx:role/datadog-role"
container_definitions = <<EOF
[
{
"name": "${var.environment}-${var.datadog-identifier}",
"network_mode" : "awsvpc",
"image": "datadog/agent:latest",
"portMappings": [
{
...
Run Code Online (Sandbox Code Playgroud)
我的服务定义:
resource "aws_ecs_service" "datadog" {
name = "${var.environment}-${var.datadog-identifier}-datadog-ecs-service"
cluster = "${var.cluster}"
task_definition = "${aws_ecs_task_definition.datadog.arn}"
network_configuration {
subnets = flatten(["${var.private_subnet_ids}"])
}
# This allows running one for every instance
scheduling_strategy = "DAEMON"
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误 -
InvalidParameterException: Network Configuration is not valid for the given networkMode of this task definition
Run Code Online (Sandbox Code Playgroud)
我在这里缺少什么吗?查看 Terraform 文档和 GitHub 问题,这应该可行。它与将 …
amazon-web-services amazon-ecs terraform terraform-provider-aws
我正在尝试将 lambda 函数的日志写入由 terraform 创建的 CloudWatch 日志组。
这是 lambda 策略 json -
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1580216411252",
"Action": [
"logs:CreateLogStream",
"logs:CreateLogDelivery",
"logs:PutLogEvents"
],
"Effect": "Allow",
"Resource": "arn:aws:logs:*:*:*"
}
]
}
Run Code Online (Sandbox Code Playgroud)
这是 lambda 假设策略 json -
{
"Version": "2012-10-17",
"Statement": [{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}]
}
Run Code Online (Sandbox Code Playgroud)
我已将其添加到 lambda.tf 文件中 -
resource "aws_cloudwatch_log_group" "example" {
name = "/test/logs/${var.lambda_function_name}"
}
Run Code Online (Sandbox Code Playgroud)
尽管 CloudWatch 日志组“/test/logs/${var.lambda_function_name}”是通过 terraform 创建的,但我无法将 lambda 函数的日志写入该组。
如果我将 lambda 策略 json 更改为 …
amazon-web-services aws-lambda terraform terraform-provider-aws
我使用在 Docker Compose 上运行的开源服务器应用程序。它有一些服务,包括 PostgreSQL DB 和 Redis。
如何使用 Terraform 在完整的 IaC 中最好地将此应用程序部署到 AWS?
ecs-cliecs-cli现在支持docker compose在 Amazon ECS 中发送配置。
但是,我不认为它可以与 Terraform 工作流程集成(这可能不是什么大惊小怪)。我知道肯定是,ecs-cli是不是在CloudFormation支持,按照这一问题在这个时候,仍然打开。所以我认为它也不能轻易添加到 Terraform。
docker-compose.yml文件,将其转换为kubectlYAML。但这还不是完全的 IaC。每次 docker-compose 在源存储库中更改时,您都必须重新翻译您的配置。这听起来像很多工作。
helm 提供程序运行 Terraform 以在集群上安装带有 Helm …docker-compose kubernetes-helm apache-superset terraform-provider-aws amazon-eks
我的目录结构
\n\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 ec2\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 ec2.tf\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 outputs.tf\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 vars.tf\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.tf\nRun Code Online (Sandbox Code Playgroud)\n\n主.tf
\n\nprovider "aws" {\n region = "us-east-1"\n}\n\nmodule "ec2" {\n source = "./ec2"\n}\nRun Code Online (Sandbox Code Playgroud)\n\nec2/ec2.tf
\n\ndata "aws_ami" "example" {\n most_recent = true\n owners = [\n "amazon"]\n\n filter {\n name = "image-id"\n values = [\n "ami-0323c3dd2da7fb37d"]\n }\n\n filter {\n name = "root-device-type"\n values = [\n "ebs"]\n }\n\n filter {\n name = "virtualization-type"\n values = [\n "hvm"]\n }\n}\n\nresource "aws_instance" "web" {\n ami = data.aws_ami.example.id\n instance_type = "t2.micro"\n subnet_id …Run Code Online (Sandbox Code Playgroud) 我的简单 terraform 文件是:
provider "aws" {
region = "region"
access_key = "key"
secret_key = "secret_key"
}
terraform {
backend "s3" {
# Replace this with your bucket name!
bucket = "great-name-terraform-state-2"
key = "global/s3/terraform.tfstate"
region = "eu-central-1"
# Replace this with your DynamoDB table name!
dynamodb_table = "great-name-locks-2"
encrypt = true
}
}
resource "aws_s3_bucket" "terraform_state" {
bucket = "great-name-terraform-state-2"
# Enable versioning so we can see the full revision history of our
# state files
versioning {
enabled = …Run Code Online (Sandbox Code Playgroud)