We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Cloudformation bugs in EMR_Cluster example
Great project!

I found 2 bugs with the EMR cloudformation template. 

More than happy to submit a PR with the corrections if considered. 

1. The reference to `EMR_AutoScaling_DefaultRole` fails template validation checks with the current configuration.

```
https://github.com/charlesgreen/troposphere/blob/master/examples/EMR_Cluster.py#L205

Bug:
AutoScalingRole=Ref(emr_autoscaling_role),

Correction:
AutoScalingRole=emr_autoscaling_role,
```

2. MasterInstanceGroup should not have an associated autoscaling group.

I receive an error and found the following in the AWS documentation. 
"Each instance group in a cluster, except the master instance group, can have its own scaling policy, which consists of scale-out and scale-in rules".

https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-automatic-scaling.html

```
https://github.com/charlesgreen/troposphere/blob/master/examples/EMR_Cluster.py#L214

# lines 214 to 220 should be deleted

AutoScalingPolicy=emr.AutoScalingPolicy(
    Constraints=emr.ScalingConstraints(
      MinCapacity="1",
      MaxCapacity="3"
    ),
  Rules=generate_rules("MasterAutoScalingPolicy")
)
```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `examples/EMR_Cluster.py`
Content:
```
1 from troposphere import Parameter, Ref, Template, Tags, If, Equals, Not, Join
2 from troposphere.constants import KEY_PAIR_NAME, SUBNET_ID, M4_LARGE, NUMBER
3 import troposphere.emr as emr
4 import troposphere.iam as iam
5 
6 
7 scaling_policy = emr.SimpleScalingPolicyConfiguration(
8                     AdjustmentType="EXACT_CAPACITY",
9                     ScalingAdjustment="1",
10                     CoolDown="300"
11                 )
12 
13 
14 kms_key = 'arn:aws:kms:us-east-1:123456789012:key/1234-1234-1234-1234-1234'
15 
16 security_configuration = {
17     'EncryptionConfiguration': {
18         'EnableInTransitEncryption': 'true',
19         'InTransitEncryptionConfiguration': {
20             'TLSCertificateConfiguration': {
21                 'CertificateProviderType': 'PEM',
22                 'S3Object': 's3://MyConfigStore/artifacts/MyCerts.zip'
23             }
24         },
25         'EnableAtRestEncryption': 'true',
26         'AtRestEncryptionConfiguration': {
27             'S3EncryptionConfiguration': {
28                 'EncryptionMode': 'SSE-KMS',
29                 'AwsKmsKey': kms_key
30             },
31             'LocalDiskEncryptionConfiguration': {
32                 'EncryptionKeyProviderType': 'AwsKms',
33                 'AwsKmsKey': kms_key
34             }
35         }
36     }
37 }
38 
39 
40 def generate_rules(rules_name):
41     global emr, scaling_policy
42 
43     rules = [
44         emr.ScalingRule(
45             Name=rules_name,
46             Description="%s rules" % rules_name,
47             Action=emr.ScalingAction(
48                 Market="ON_DEMAND",
49                 SimpleScalingPolicyConfiguration=scaling_policy
50             ),
51             Trigger=emr.ScalingTrigger(
52                 CloudWatchAlarmDefinition=emr.CloudWatchAlarmDefinition(
53                     ComparisonOperator="GREATER_THAN",
54                     EvaluationPeriods="120",
55                     MetricName="TestMetric",
56                     Namespace="AWS/ElasticMapReduce",
57                     Period="300",
58                     Statistic="AVERAGE",
59                     Threshold="50",
60                     Unit="PERCENT",
61                     Dimensions=[
62                         emr.MetricDimension(
63                             'my.custom.master.property',
64                             'my.custom.master.value'
65                         )
66                     ]
67                 )
68             )
69         )
70     ]
71     return rules
72 
73 
74 template = Template()
75 template.add_description(
76     "Sample CloudFormation template for creating an EMR cluster"
77 )
78 
79 keyname = template.add_parameter(Parameter(
80     "KeyName",
81     Description="Name of an existing EC2 KeyPair to enable SSH "
82                 "to the instances",
83     Type=KEY_PAIR_NAME
84 ))
85 
86 subnet = template.add_parameter(Parameter(
87     "Subnet",
88     Description="Subnet ID for creating the EMR cluster",
89     Type=SUBNET_ID
90 ))
91 
92 spot = template.add_parameter(Parameter(
93     "SpotPrice",
94     Description="Spot price (or use 0 for 'on demand' instance)",
95     Type=NUMBER,
96     Default="0.1"
97 ))
98 
99 withSpotPrice = "WithSpotPrice"
100 template.add_condition(withSpotPrice, Not(Equals(Ref(spot), "0")))
101 
102 gcTimeRatio = template.add_parameter(Parameter(
103     "GcTimeRatioValue",
104     Description="Hadoop name node garbage collector time ratio",
105     Type=NUMBER,
106     Default="19"
107 ))
108 
109 # IAM roles required by EMR
110 
111 emr_service_role = template.add_resource(iam.Role(
112     'EMRServiceRole',
113     AssumeRolePolicyDocument={
114         "Statement": [{
115             "Effect": "Allow",
116             "Principal": {
117                 "Service": [
118                     "elasticmapreduce.amazonaws.com"
119                 ]
120             },
121             "Action": ["sts:AssumeRole"]
122         }]
123     },
124     ManagedPolicyArns=[
125         'arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceRole'
126     ]
127 ))
128 
129 emr_autoscaling_role = "EMR_AutoScaling_DefaultRole"
130 
131 emr_job_flow_role = template.add_resource(iam.Role(
132     "EMRJobFlowRole",
133     AssumeRolePolicyDocument={
134         "Statement": [{
135             "Effect": "Allow",
136             "Principal": {
137                 "Service": [
138                     "ec2.amazonaws.com"
139                 ]
140             },
141             "Action": ["sts:AssumeRole"]
142         }]
143     },
144     ManagedPolicyArns=[
145         'arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceforEC2Role'
146     ]
147 ))
148 
149 emr_instance_profile = template.add_resource(iam.InstanceProfile(
150     "EMRInstanceProfile",
151     Roles=[Ref(emr_job_flow_role)]
152 ))
153 
154 # EMR Cluster Resource
155 
156 security_config = template.add_resource(emr.SecurityConfiguration(
157     'EMRSecurityConfiguration',
158     Name="EMRSampleClusterSecurityConfiguration",
159     SecurityConfiguration=security_configuration,
160 ))
161 
162 cluster = template.add_resource(emr.Cluster(
163     "EMRSampleCluster",
164     Name="EMR Sample Cluster",
165     ReleaseLabel='emr-4.4.0',
166     SecurityConfiguration=Ref(security_config),
167     BootstrapActions=[emr.BootstrapActionConfig(
168         Name='Dummy bootstrap action',
169         ScriptBootstrapAction=emr.ScriptBootstrapActionConfig(
170             Path='file:/usr/share/aws/emr/scripts/install-hue',
171             Args=["dummy", "parameter"]
172         )
173     )],
174     Configurations=[
175         emr.Configuration(
176             Classification="core-site",
177             ConfigurationProperties={
178                 'hadoop.security.groups.cache.secs': '250'
179             }
180         ),
181         emr.Configuration(
182             Classification="mapred-site",
183             ConfigurationProperties={
184                 'mapred.tasktracker.map.tasks.maximum': '2',
185                 'mapreduce.map.sort.spill.percent': '90',
186                 'mapreduce.tasktracker.reduce.tasks.maximum': '5'
187             }
188         ),
189         emr.Configuration(
190             Classification="hadoop-env",
191             Configurations=[
192                 emr.Configuration(
193                     Classification="export",
194                     ConfigurationProperties={
195                         "HADOOP_DATANODE_HEAPSIZE": "2048",
196                         "HADOOP_NAMENODE_OPTS": Join("", ["-XX:GCTimeRatio=",
197                                                           Ref(gcTimeRatio)])
198                     }
199                 )
200             ]
201         )
202     ],
203     JobFlowRole=Ref(emr_instance_profile),
204     ServiceRole=Ref(emr_service_role),
205     AutoScalingRole=Ref(emr_autoscaling_role),
206     Instances=emr.JobFlowInstancesConfig(
207         Ec2KeyName=Ref(keyname),
208         Ec2SubnetId=Ref(subnet),
209         MasterInstanceGroup=emr.InstanceGroupConfigProperty(
210             Name="Master Instance",
211             InstanceCount="1",
212             InstanceType=M4_LARGE,
213             Market="ON_DEMAND",
214             AutoScalingPolicy=emr.AutoScalingPolicy(
215                 Constraints=emr.ScalingConstraints(
216                     MinCapacity="1",
217                     MaxCapacity="3"
218                 ),
219                 Rules=generate_rules("MasterAutoScalingPolicy")
220             )
221         ),
222         CoreInstanceGroup=emr.InstanceGroupConfigProperty(
223             Name="Core Instance",
224             BidPrice=If(withSpotPrice, Ref(spot), Ref("AWS::NoValue")),
225             Market=If(withSpotPrice, "SPOT", "ON_DEMAND"),
226             AutoScalingPolicy=emr.AutoScalingPolicy(
227                 Constraints=emr.ScalingConstraints(
228                     MinCapacity="1",
229                     MaxCapacity="3"
230                 ),
231                 Rules=generate_rules("CoreAutoScalingPolicy"),
232             ),
233             EbsConfiguration=emr.EbsConfiguration(
234                 EbsBlockDeviceConfigs=[
235                     emr.EbsBlockDeviceConfigs(
236                         VolumeSpecification=emr.VolumeSpecification(
237                             SizeInGB="10",
238                             VolumeType="gp2"
239                         ),
240                         VolumesPerInstance="1"
241                     )
242                 ],
243                 EbsOptimized="true"
244             ),
245             InstanceCount="1",
246             InstanceType=M4_LARGE,
247         )
248     ),
249     Applications=[
250         emr.Application(Name="Hadoop"),
251         emr.Application(Name="Hive"),
252         emr.Application(Name="Mahout"),
253         emr.Application(Name="Pig"),
254         emr.Application(Name="Spark")
255     ],
256     VisibleToAllUsers="true",
257     Tags=Tags(
258         Name="EMR Sample Cluster"
259     )
260 ))
261 
262 step = template.add_resource(emr.Step(
263     'TestStep',
264     Name="TestStep",
265     ActionOnFailure='CONTINUE',
266     HadoopJarStep=emr.HadoopJarStepConfig(
267         Args=["5", "10"],
268         Jar="s3://emr-cfn-test/hadoop-mapreduce-examples-2.6.0.jar",
269         MainClass="pi",
270         StepProperties=[
271             emr.KeyValue('my.custom.property', 'my.custom.value')
272         ]
273     ),
274     JobFlowId=Ref(cluster)
275 ))
276 
277 print(template.to_json())
278
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/examples/EMR_Cluster.py b/examples/EMR_Cluster.py
--- a/examples/EMR_Cluster.py
+++ b/examples/EMR_Cluster.py
@@ -202,7 +202,7 @@
     ],
     JobFlowRole=Ref(emr_instance_profile),
     ServiceRole=Ref(emr_service_role),
-    AutoScalingRole=Ref(emr_autoscaling_role),
+    AutoScalingRole=emr_autoscaling_role,
     Instances=emr.JobFlowInstancesConfig(
         Ec2KeyName=Ref(keyname),
         Ec2SubnetId=Ref(subnet),

{"golden_diff": "diff --git a/examples/EMR_Cluster.py b/examples/EMR_Cluster.py\n--- a/examples/EMR_Cluster.py\n+++ b/examples/EMR_Cluster.py\n@@ -202,7 +202,7 @@\n     ],\n     JobFlowRole=Ref(emr_instance_profile),\n     ServiceRole=Ref(emr_service_role),\n-    AutoScalingRole=Ref(emr_autoscaling_role),\n+    AutoScalingRole=emr_autoscaling_role,\n     Instances=emr.JobFlowInstancesConfig(\n         Ec2KeyName=Ref(keyname),\n         Ec2SubnetId=Ref(subnet),\n", "issue": "Cloudformation bugs in EMR_Cluster example\nGreat project!\r\n\r\nI found 2 bugs with the EMR cloudformation template. \r\n\r\nMore than happy to submit a PR with the corrections if considered. \r\n\r\n1. The reference to `EMR_AutoScaling_DefaultRole` fails template validation checks with the current configuration.\r\n\r\n```\r\nhttps://github.com/charlesgreen/troposphere/blob/master/examples/EMR_Cluster.py#L205\r\n\r\nBug:\r\nAutoScalingRole=Ref(emr_autoscaling_role),\r\n\r\nCorrection:\r\nAutoScalingRole=emr_autoscaling_role,\r\n```\r\n\r\n2. MasterInstanceGroup should not have an associated autoscaling group.\r\n\r\nI receive an error and found the following in the AWS documentation. \r\n\"Each instance group in a cluster, except the master instance group, can have its own scaling policy, which consists of scale-out and scale-in rules\".\r\n\r\nhttps://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-automatic-scaling.html\r\n\r\n```\r\nhttps://github.com/charlesgreen/troposphere/blob/master/examples/EMR_Cluster.py#L214\r\n\r\n# lines 214 to 220 should be deleted\r\n\r\nAutoScalingPolicy=emr.AutoScalingPolicy(\r\n    Constraints=emr.ScalingConstraints(\r\n      MinCapacity=\"1\",\r\n      MaxCapacity=\"3\"\r\n    ),\r\n  Rules=generate_rules(\"MasterAutoScalingPolicy\")\r\n)\r\n```\r\n\n", "before_files": [{"content": "from troposphere import Parameter, Ref, Template, Tags, If, Equals, Not, Join\nfrom troposphere.constants import KEY_PAIR_NAME, SUBNET_ID, M4_LARGE, NUMBER\nimport troposphere.emr as emr\nimport troposphere.iam as iam\n\n\nscaling_policy = emr.SimpleScalingPolicyConfiguration(\n                    AdjustmentType=\"EXACT_CAPACITY\",\n                    ScalingAdjustment=\"1\",\n                    CoolDown=\"300\"\n                )\n\n\nkms_key = 'arn:aws:kms:us-east-1:123456789012:key/1234-1234-1234-1234-1234'\n\nsecurity_configuration = {\n    'EncryptionConfiguration': {\n        'EnableInTransitEncryption': 'true',\n        'InTransitEncryptionConfiguration': {\n            'TLSCertificateConfiguration': {\n                'CertificateProviderType': 'PEM',\n                'S3Object': 's3://MyConfigStore/artifacts/MyCerts.zip'\n            }\n        },\n        'EnableAtRestEncryption': 'true',\n        'AtRestEncryptionConfiguration': {\n            'S3EncryptionConfiguration': {\n                'EncryptionMode': 'SSE-KMS',\n                'AwsKmsKey': kms_key\n            },\n            'LocalDiskEncryptionConfiguration': {\n                'EncryptionKeyProviderType': 'AwsKms',\n                'AwsKmsKey': kms_key\n            }\n        }\n    }\n}\n\n\ndef generate_rules(rules_name):\n    global emr, scaling_policy\n\n    rules = [\n        emr.ScalingRule(\n            Name=rules_name,\n            Description=\"%s rules\" % rules_name,\n            Action=emr.ScalingAction(\n                Market=\"ON_DEMAND\",\n                SimpleScalingPolicyConfiguration=scaling_policy\n            ),\n            Trigger=emr.ScalingTrigger(\n                CloudWatchAlarmDefinition=emr.CloudWatchAlarmDefinition(\n                    ComparisonOperator=\"GREATER_THAN\",\n                    EvaluationPeriods=\"120\",\n                    MetricName=\"TestMetric\",\n                    Namespace=\"AWS/ElasticMapReduce\",\n                    Period=\"300\",\n                    Statistic=\"AVERAGE\",\n                    Threshold=\"50\",\n                    Unit=\"PERCENT\",\n                    Dimensions=[\n                        emr.MetricDimension(\n                            'my.custom.master.property',\n                            'my.custom.master.value'\n                        )\n                    ]\n                )\n            )\n        )\n    ]\n    return rules\n\n\ntemplate = Template()\ntemplate.add_description(\n    \"Sample CloudFormation template for creating an EMR cluster\"\n)\n\nkeyname = template.add_parameter(Parameter(\n    \"KeyName\",\n    Description=\"Name of an existing EC2 KeyPair to enable SSH \"\n                \"to the instances\",\n    Type=KEY_PAIR_NAME\n))\n\nsubnet = template.add_parameter(Parameter(\n    \"Subnet\",\n    Description=\"Subnet ID for creating the EMR cluster\",\n    Type=SUBNET_ID\n))\n\nspot = template.add_parameter(Parameter(\n    \"SpotPrice\",\n    Description=\"Spot price (or use 0 for 'on demand' instance)\",\n    Type=NUMBER,\n    Default=\"0.1\"\n))\n\nwithSpotPrice = \"WithSpotPrice\"\ntemplate.add_condition(withSpotPrice, Not(Equals(Ref(spot), \"0\")))\n\ngcTimeRatio = template.add_parameter(Parameter(\n    \"GcTimeRatioValue\",\n    Description=\"Hadoop name node garbage collector time ratio\",\n    Type=NUMBER,\n    Default=\"19\"\n))\n\n# IAM roles required by EMR\n\nemr_service_role = template.add_resource(iam.Role(\n    'EMRServiceRole',\n    AssumeRolePolicyDocument={\n        \"Statement\": [{\n            \"Effect\": \"Allow\",\n            \"Principal\": {\n                \"Service\": [\n                    \"elasticmapreduce.amazonaws.com\"\n                ]\n            },\n            \"Action\": [\"sts:AssumeRole\"]\n        }]\n    },\n    ManagedPolicyArns=[\n        'arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceRole'\n    ]\n))\n\nemr_autoscaling_role = \"EMR_AutoScaling_DefaultRole\"\n\nemr_job_flow_role = template.add_resource(iam.Role(\n    \"EMRJobFlowRole\",\n    AssumeRolePolicyDocument={\n        \"Statement\": [{\n            \"Effect\": \"Allow\",\n            \"Principal\": {\n                \"Service\": [\n                    \"ec2.amazonaws.com\"\n                ]\n            },\n            \"Action\": [\"sts:AssumeRole\"]\n        }]\n    },\n    ManagedPolicyArns=[\n        'arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceforEC2Role'\n    ]\n))\n\nemr_instance_profile = template.add_resource(iam.InstanceProfile(\n    \"EMRInstanceProfile\",\n    Roles=[Ref(emr_job_flow_role)]\n))\n\n# EMR Cluster Resource\n\nsecurity_config = template.add_resource(emr.SecurityConfiguration(\n    'EMRSecurityConfiguration',\n    Name=\"EMRSampleClusterSecurityConfiguration\",\n    SecurityConfiguration=security_configuration,\n))\n\ncluster = template.add_resource(emr.Cluster(\n    \"EMRSampleCluster\",\n    Name=\"EMR Sample Cluster\",\n    ReleaseLabel='emr-4.4.0',\n    SecurityConfiguration=Ref(security_config),\n    BootstrapActions=[emr.BootstrapActionConfig(\n        Name='Dummy bootstrap action',\n        ScriptBootstrapAction=emr.ScriptBootstrapActionConfig(\n            Path='file:/usr/share/aws/emr/scripts/install-hue',\n            Args=[\"dummy\", \"parameter\"]\n        )\n    )],\n    Configurations=[\n        emr.Configuration(\n            Classification=\"core-site\",\n            ConfigurationProperties={\n                'hadoop.security.groups.cache.secs': '250'\n            }\n        ),\n        emr.Configuration(\n            Classification=\"mapred-site\",\n            ConfigurationProperties={\n                'mapred.tasktracker.map.tasks.maximum': '2',\n                'mapreduce.map.sort.spill.percent': '90',\n                'mapreduce.tasktracker.reduce.tasks.maximum': '5'\n            }\n        ),\n        emr.Configuration(\n            Classification=\"hadoop-env\",\n            Configurations=[\n                emr.Configuration(\n                    Classification=\"export\",\n                    ConfigurationProperties={\n                        \"HADOOP_DATANODE_HEAPSIZE\": \"2048\",\n                        \"HADOOP_NAMENODE_OPTS\": Join(\"\", [\"-XX:GCTimeRatio=\",\n                                                          Ref(gcTimeRatio)])\n                    }\n                )\n            ]\n        )\n    ],\n    JobFlowRole=Ref(emr_instance_profile),\n    ServiceRole=Ref(emr_service_role),\n    AutoScalingRole=Ref(emr_autoscaling_role),\n    Instances=emr.JobFlowInstancesConfig(\n        Ec2KeyName=Ref(keyname),\n        Ec2SubnetId=Ref(subnet),\n        MasterInstanceGroup=emr.InstanceGroupConfigProperty(\n            Name=\"Master Instance\",\n            InstanceCount=\"1\",\n            InstanceType=M4_LARGE,\n            Market=\"ON_DEMAND\",\n            AutoScalingPolicy=emr.AutoScalingPolicy(\n                Constraints=emr.ScalingConstraints(\n                    MinCapacity=\"1\",\n                    MaxCapacity=\"3\"\n                ),\n                Rules=generate_rules(\"MasterAutoScalingPolicy\")\n            )\n        ),\n        CoreInstanceGroup=emr.InstanceGroupConfigProperty(\n            Name=\"Core Instance\",\n            BidPrice=If(withSpotPrice, Ref(spot), Ref(\"AWS::NoValue\")),\n            Market=If(withSpotPrice, \"SPOT\", \"ON_DEMAND\"),\n            AutoScalingPolicy=emr.AutoScalingPolicy(\n                Constraints=emr.ScalingConstraints(\n                    MinCapacity=\"1\",\n                    MaxCapacity=\"3\"\n                ),\n                Rules=generate_rules(\"CoreAutoScalingPolicy\"),\n            ),\n            EbsConfiguration=emr.EbsConfiguration(\n                EbsBlockDeviceConfigs=[\n                    emr.EbsBlockDeviceConfigs(\n                        VolumeSpecification=emr.VolumeSpecification(\n                            SizeInGB=\"10\",\n                            VolumeType=\"gp2\"\n                        ),\n                        VolumesPerInstance=\"1\"\n                    )\n                ],\n                EbsOptimized=\"true\"\n            ),\n            InstanceCount=\"1\",\n            InstanceType=M4_LARGE,\n        )\n    ),\n    Applications=[\n        emr.Application(Name=\"Hadoop\"),\n        emr.Application(Name=\"Hive\"),\n        emr.Application(Name=\"Mahout\"),\n        emr.Application(Name=\"Pig\"),\n        emr.Application(Name=\"Spark\")\n    ],\n    VisibleToAllUsers=\"true\",\n    Tags=Tags(\n        Name=\"EMR Sample Cluster\"\n    )\n))\n\nstep = template.add_resource(emr.Step(\n    'TestStep',\n    Name=\"TestStep\",\n    ActionOnFailure='CONTINUE',\n    HadoopJarStep=emr.HadoopJarStepConfig(\n        Args=[\"5\", \"10\"],\n        Jar=\"s3://emr-cfn-test/hadoop-mapreduce-examples-2.6.0.jar\",\n        MainClass=\"pi\",\n        StepProperties=[\n            emr.KeyValue('my.custom.property', 'my.custom.value')\n        ]\n    ),\n    JobFlowId=Ref(cluster)\n))\n\nprint(template.to_json())\n", "path": "examples/EMR_Cluster.py"}], "after_files": [{"content": "from troposphere import Parameter, Ref, Template, Tags, If, Equals, Not, Join\nfrom troposphere.constants import KEY_PAIR_NAME, SUBNET_ID, M4_LARGE, NUMBER\nimport troposphere.emr as emr\nimport troposphere.iam as iam\n\n\nscaling_policy = emr.SimpleScalingPolicyConfiguration(\n                    AdjustmentType=\"EXACT_CAPACITY\",\n                    ScalingAdjustment=\"1\",\n                    CoolDown=\"300\"\n                )\n\n\nkms_key = 'arn:aws:kms:us-east-1:123456789012:key/1234-1234-1234-1234-1234'\n\nsecurity_configuration = {\n    'EncryptionConfiguration': {\n        'EnableInTransitEncryption': 'true',\n        'InTransitEncryptionConfiguration': {\n            'TLSCertificateConfiguration': {\n                'CertificateProviderType': 'PEM',\n                'S3Object': 's3://MyConfigStore/artifacts/MyCerts.zip'\n            }\n        },\n        'EnableAtRestEncryption': 'true',\n        'AtRestEncryptionConfiguration': {\n            'S3EncryptionConfiguration': {\n                'EncryptionMode': 'SSE-KMS',\n                'AwsKmsKey': kms_key\n            },\n            'LocalDiskEncryptionConfiguration': {\n                'EncryptionKeyProviderType': 'AwsKms',\n                'AwsKmsKey': kms_key\n            }\n        }\n    }\n}\n\n\ndef generate_rules(rules_name):\n    global emr, scaling_policy\n\n    rules = [\n        emr.ScalingRule(\n            Name=rules_name,\n            Description=\"%s rules\" % rules_name,\n            Action=emr.ScalingAction(\n                Market=\"ON_DEMAND\",\n                SimpleScalingPolicyConfiguration=scaling_policy\n            ),\n            Trigger=emr.ScalingTrigger(\n                CloudWatchAlarmDefinition=emr.CloudWatchAlarmDefinition(\n                    ComparisonOperator=\"GREATER_THAN\",\n                    EvaluationPeriods=\"120\",\n                    MetricName=\"TestMetric\",\n                    Namespace=\"AWS/ElasticMapReduce\",\n                    Period=\"300\",\n                    Statistic=\"AVERAGE\",\n                    Threshold=\"50\",\n                    Unit=\"PERCENT\",\n                    Dimensions=[\n                        emr.MetricDimension(\n                            'my.custom.master.property',\n                            'my.custom.master.value'\n                        )\n                    ]\n                )\n            )\n        )\n    ]\n    return rules\n\n\ntemplate = Template()\ntemplate.add_description(\n    \"Sample CloudFormation template for creating an EMR cluster\"\n)\n\nkeyname = template.add_parameter(Parameter(\n    \"KeyName\",\n    Description=\"Name of an existing EC2 KeyPair to enable SSH \"\n                \"to the instances\",\n    Type=KEY_PAIR_NAME\n))\n\nsubnet = template.add_parameter(Parameter(\n    \"Subnet\",\n    Description=\"Subnet ID for creating the EMR cluster\",\n    Type=SUBNET_ID\n))\n\nspot = template.add_parameter(Parameter(\n    \"SpotPrice\",\n    Description=\"Spot price (or use 0 for 'on demand' instance)\",\n    Type=NUMBER,\n    Default=\"0.1\"\n))\n\nwithSpotPrice = \"WithSpotPrice\"\ntemplate.add_condition(withSpotPrice, Not(Equals(Ref(spot), \"0\")))\n\ngcTimeRatio = template.add_parameter(Parameter(\n    \"GcTimeRatioValue\",\n    Description=\"Hadoop name node garbage collector time ratio\",\n    Type=NUMBER,\n    Default=\"19\"\n))\n\n# IAM roles required by EMR\n\nemr_service_role = template.add_resource(iam.Role(\n    'EMRServiceRole',\n    AssumeRolePolicyDocument={\n        \"Statement\": [{\n            \"Effect\": \"Allow\",\n            \"Principal\": {\n                \"Service\": [\n                    \"elasticmapreduce.amazonaws.com\"\n                ]\n            },\n            \"Action\": [\"sts:AssumeRole\"]\n        }]\n    },\n    ManagedPolicyArns=[\n        'arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceRole'\n    ]\n))\n\nemr_autoscaling_role = \"EMR_AutoScaling_DefaultRole\"\n\nemr_job_flow_role = template.add_resource(iam.Role(\n    \"EMRJobFlowRole\",\n    AssumeRolePolicyDocument={\n        \"Statement\": [{\n            \"Effect\": \"Allow\",\n            \"Principal\": {\n                \"Service\": [\n                    \"ec2.amazonaws.com\"\n                ]\n            },\n            \"Action\": [\"sts:AssumeRole\"]\n        }]\n    },\n    ManagedPolicyArns=[\n        'arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceforEC2Role'\n    ]\n))\n\nemr_instance_profile = template.add_resource(iam.InstanceProfile(\n    \"EMRInstanceProfile\",\n    Roles=[Ref(emr_job_flow_role)]\n))\n\n# EMR Cluster Resource\n\nsecurity_config = template.add_resource(emr.SecurityConfiguration(\n    'EMRSecurityConfiguration',\n    Name=\"EMRSampleClusterSecurityConfiguration\",\n    SecurityConfiguration=security_configuration,\n))\n\ncluster = template.add_resource(emr.Cluster(\n    \"EMRSampleCluster\",\n    Name=\"EMR Sample Cluster\",\n    ReleaseLabel='emr-4.4.0',\n    SecurityConfiguration=Ref(security_config),\n    BootstrapActions=[emr.BootstrapActionConfig(\n        Name='Dummy bootstrap action',\n        ScriptBootstrapAction=emr.ScriptBootstrapActionConfig(\n            Path='file:/usr/share/aws/emr/scripts/install-hue',\n            Args=[\"dummy\", \"parameter\"]\n        )\n    )],\n    Configurations=[\n        emr.Configuration(\n            Classification=\"core-site\",\n            ConfigurationProperties={\n                'hadoop.security.groups.cache.secs': '250'\n            }\n        ),\n        emr.Configuration(\n            Classification=\"mapred-site\",\n            ConfigurationProperties={\n                'mapred.tasktracker.map.tasks.maximum': '2',\n                'mapreduce.map.sort.spill.percent': '90',\n                'mapreduce.tasktracker.reduce.tasks.maximum': '5'\n            }\n        ),\n        emr.Configuration(\n            Classification=\"hadoop-env\",\n            Configurations=[\n                emr.Configuration(\n                    Classification=\"export\",\n                    ConfigurationProperties={\n                        \"HADOOP_DATANODE_HEAPSIZE\": \"2048\",\n                        \"HADOOP_NAMENODE_OPTS\": Join(\"\", [\"-XX:GCTimeRatio=\",\n                                                          Ref(gcTimeRatio)])\n                    }\n                )\n            ]\n        )\n    ],\n    JobFlowRole=Ref(emr_instance_profile),\n    ServiceRole=Ref(emr_service_role),\n    AutoScalingRole=emr_autoscaling_role,\n    Instances=emr.JobFlowInstancesConfig(\n        Ec2KeyName=Ref(keyname),\n        Ec2SubnetId=Ref(subnet),\n        MasterInstanceGroup=emr.InstanceGroupConfigProperty(\n            Name=\"Master Instance\",\n            InstanceCount=\"1\",\n            InstanceType=M4_LARGE,\n            Market=\"ON_DEMAND\",\n            AutoScalingPolicy=emr.AutoScalingPolicy(\n                Constraints=emr.ScalingConstraints(\n                    MinCapacity=\"1\",\n                    MaxCapacity=\"3\"\n                ),\n                Rules=generate_rules(\"MasterAutoScalingPolicy\")\n            )\n        ),\n        CoreInstanceGroup=emr.InstanceGroupConfigProperty(\n            Name=\"Core Instance\",\n            BidPrice=If(withSpotPrice, Ref(spot), Ref(\"AWS::NoValue\")),\n            Market=If(withSpotPrice, \"SPOT\", \"ON_DEMAND\"),\n            AutoScalingPolicy=emr.AutoScalingPolicy(\n                Constraints=emr.ScalingConstraints(\n                    MinCapacity=\"1\",\n                    MaxCapacity=\"3\"\n                ),\n                Rules=generate_rules(\"CoreAutoScalingPolicy\"),\n            ),\n            EbsConfiguration=emr.EbsConfiguration(\n                EbsBlockDeviceConfigs=[\n                    emr.EbsBlockDeviceConfigs(\n                        VolumeSpecification=emr.VolumeSpecification(\n                            SizeInGB=\"10\",\n                            VolumeType=\"gp2\"\n                        ),\n                        VolumesPerInstance=\"1\"\n                    )\n                ],\n                EbsOptimized=\"true\"\n            ),\n            InstanceCount=\"1\",\n            InstanceType=M4_LARGE,\n        )\n    ),\n    Applications=[\n        emr.Application(Name=\"Hadoop\"),\n        emr.Application(Name=\"Hive\"),\n        emr.Application(Name=\"Mahout\"),\n        emr.Application(Name=\"Pig\"),\n        emr.Application(Name=\"Spark\")\n    ],\n    VisibleToAllUsers=\"true\",\n    Tags=Tags(\n        Name=\"EMR Sample Cluster\"\n    )\n))\n\nstep = template.add_resource(emr.Step(\n    'TestStep',\n    Name=\"TestStep\",\n    ActionOnFailure='CONTINUE',\n    HadoopJarStep=emr.HadoopJarStepConfig(\n        Args=[\"5\", \"10\"],\n        Jar=\"s3://emr-cfn-test/hadoop-mapreduce-examples-2.6.0.jar\",\n        MainClass=\"pi\",\n        StepProperties=[\n            emr.KeyValue('my.custom.property', 'my.custom.value')\n        ]\n    ),\n    JobFlowId=Ref(cluster)\n))\n\nprint(template.to_json())\n", "path": "examples/EMR_Cluster.py"}]}