【问题标题】:Is there a way to create EMR security config with CloudFormation/Terraform有没有办法使用 CloudFormation/Terraform 创建 EMR 安全配置
【发布时间】:2025-06-20 13:30:01
【问题描述】:

我想归档类似于 CLI 命令的逻辑: aws emr create-security-configuration --name [name] --security-configuration ... 并在 Terraform 脚本中进一步使用它。

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation aws-cli amazon-emr terraform


    【解决方案1】:

    更新 06/07/2017:自 Jun 6 2017 起,AWS::EMR::SecurityConfiguration 资源现在可在 CloudFormation 中使用,自 2017 年 5 月 11 日 (v0.9.5) 起,emr_security_configuration 资源在 Terraform 中可用。


    不幸的是,目前似乎无法使用 CloudFormation 的 AWS::EMR::Cluster CloudFormation 资源或 Terraform 的 aws_emr_cluster 资源为 RunJobFlow API 指定 SecurityConfiguration,并且没有对应的资源CreateSecurityConfiguration API。

    EMR Security Configuration 功能是在 Sep 21 2016 上添加的,新功能公告与其在现有 CloudFormation 资源中的相应支持之间通常存在延迟。

    虽然 Terraform 往往更新更快,因为它是一个拥有更大开发社区的开源项目,但 aws_emr_cluster 资源仍然相对较新(发布 Oct 6 2016)。我已经打开了一个 GitHub issue 跟踪此实现的功能请求。

    作为目前的解决方法,您可以创建一个直接调用 CreateSecurityConfigurationRunJobFlow API 的 Custom Resource

    【讨论】:

      【解决方案2】:

      您可以在此处https://www.terraform.io/docs/providers/aws/r/security_group.html 和此处https://www.terraform.io/docs/providers/aws/r/emr_cluster.html 遵循基本示例。

      类似于:

      resource "aws_security_group" "sg" {
        name = "allow_all"
        description = "Allow all inbound traffic"
      
        ingress {
            from_port = 0
            to_port = 0
            protocol = "-1"
            cidr_blocks = ["0.0.0.0/0"]
        }
      
        egress {
            from_port = 0
            to_port = 0
            protocol = "-1"
            cidr_blocks = ["0.0.0.0/0"]
            prefix_list_ids = ["pl-12c4e678"]
        }
      }
      
      resource "aws_emr_cluster" "emr-test-cluster" {
        name          = "emr-test-arn"
        release_label = "emr-4.6.0"
        applications  = ["Spark"]
      
        termination_protection = false
        keep_job_flow_alive_when_no_steps = true
      
        ec2_attributes {
          subnet_id                         = "${aws_subnet.main.id}"
          emr_managed_master_security_group = "${aws_security_group.sg.id}"
          emr_managed_slave_security_group  = "${aws_security_group.sg.id}"
          instance_profile                  = "${aws_iam_instance_profile.emr_profile.arn}"
        }
      ...
      }
      

      【讨论】:

      • 问题与安全组无关。