将无Cloudformation无服务器模板转换为Terraform的最佳方法

问题描述 投票:-1回答:1

我正在尝试将serverless framework YML文件转换为Terraform脚本。

这是我第一次使用terraform,我在基础设施方面也没有太多经验。

我的T​​erraform出现一些错误,但我的问题更多是关于我的方法。

是否有更好/更简单/更智能的方法在Terraform中编写相同的无服务器yml?

也许在那里的模块会让我的生活更轻松。

Terraform

# Lambda invoke function role data
data "aws_iam_policy_document" "lambda_invoke_function_role" {
  version = "2012-10-17"
  statement {
    sid     = ""
    effect  = "Allow"
    actions = ["sts:AssumeRole"]

    principals {
      service = "lambda.amazonaws.com"
    }
  }
}

# Lambda ec2
data "aws_iam_policy_document" "ec2_lambda_policies_role" {
  version = "2012-10-17"
  statement {
    sid    = ""
    effect = "Allow"
    actions = [
      "ec2:CreateNetworkInterface",
      "ec2:DescribeNetworkInterfaces",
      "ec2:DetachNetworkInterface",
      "ec2:DeleteNetworkInterface"
    ]
    resources = "*"
  }
}

# Lambda allow invoke
data "aws_iam_policy_document" "allow_invoke_role" {
  version = "2012-10-17"
  statement {
    sid    = ""
    effect = "Allow"
    actions = [
      "lambda:InvokeFunction"
    ]
    resources = "*"
  }
}


# Lambda invoke function role
resource "aws_iam_role" "lambda_invoke_function_role" {
  name               = "lambdaRole"
  assume_role_policy = "${data.aws_iam_policy_document.lambda_invoke_function_role.json}"
}


# EC2 ##############################################################################
resource "aws_iam_policy" "ec2_lambda_policies_policy" {
  name               = "ec2LambdaPolicy"
  assume_role_policy = "${data.aws_iam_policy_document.ec2_lambda_policies_role.json}"
}

resource "aws_iam_role_policy_attachment" "ec2_lambda_policies_policy_attachment" {
  role       = "${aws_iam_role.lambda_invoke_function_role.name}"
  policy_arn = "${aws_iam_policy.ec2_lambda_policies_policy.arn}"
}
###############################################################################


# Allow Invoke #####################################################################
resource "aws_iam_policy" "allow_invoke_policy" {
  name               = "allowInvokePolicy"
  assume_role_policy = "${data.aws_iam_policy_document.allow_invoke_role.json}"
}

resource "aws_iam_role_policy_attachment" "allow_invoke_policy_attachment" {
  role       = "${aws_iam_role.lambda_invoke_function_role.name}"
  policy_arn = "${aws_iam_policy.allow_invoke_policy.arn}"
}
###############################################################################


# Lambda invoke function role policy attachment
resource "aws_iam_role_policy_attachment" "aws_lambda_basic_execution_role" {
  role       = "${aws_iam_role.lambda_invoke_function_role.name}"
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}

# Lambda invoke function role policy attachment
resource "aws_iam_role_policy_attachment" "aws_lambda_vpc_access_execution_role" {
  role       = "${aws_iam_role.lambda_invoke_function_role.name}"
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
}

无服务器YML(cloudformation)

  LambdaRole:
    Type: AWS::IAM::Role
    Properties:
      Path: '/'
      RoleName: LambdaRole
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action: sts:AssumeRole
      Policies:
        - PolicyName: ec2LambdaPolicies
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - ec2:CreateNetworkInterface
                  - ec2:DescribeNetworkInterfaces
                  - ec2:DetachNetworkInterface
                  - ec2:DeleteNetworkInterface
                Resource: "*"
        - PolicyName: 'AllowInvoke'
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: 'Allow'
                Action: 'lambda:InvokeFunction'
                Resource: '*'
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
        - arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
amazon-web-services terraform serverless-framework
1个回答
0
投票

您可能想看看cf-to-tf,它将云形式转换为地形。

cf-to-tf --stack foobarbaz config | json2hcl | cf-to-tf clean-hcl | terraform fmt -

这将从AWS导入堆栈foobarbaz并将其转换为terraform并将其打印到stdout。

© www.soinside.com 2019 - 2024. All rights reserved.