Skip to content

Commit cc92c16

Browse files
anton-kachurinAntonactions-bot
authored
Allow to restrict S3 access (#143)
* Allow to extend EC2 policy document * Allow to use modern IAM policy for SSM access * Fix version constraint for example script * Add new features usage to example scripts * Format files * Update readme file * Add better sample * Updated README.md Co-authored-by: Anton <akachurin@kitewheel.com> Co-authored-by: actions-bot <58130806+actions-bot@users.noreply.github.com>
1 parent 44bbccd commit cc92c16

File tree

6 files changed

+43
-4
lines changed

6 files changed

+43
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ Available targets:
175175
176176
```
177177
<!-- markdownlint-restore -->
178+
<!-- markdownlint-disable -->
178179
## Requirements
179180

180181
| Name | Version |
@@ -229,6 +230,7 @@ Available targets:
229230
| env\_vars | Map of custom ENV variables to be provided to the application running on Elastic Beanstalk, e.g. env\_vars = { DB\_USER = 'admin' DB\_PASS = 'xxxxxx' } | `map(string)` | `{}` | no |
230231
| environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | `string` | `""` | no |
231232
| environment\_type | Environment type, e.g. 'LoadBalanced' or 'SingleInstance'. If setting to 'SingleInstance', `rolling_update_type` must be set to 'Time', `updating_min_in_service` must be set to 0, and `loadbalancer_subnets` will be unused (it applies to the ELB, which does not exist in SingleInstance environments) | `string` | `"LoadBalanced"` | no |
233+
| extended\_ec2\_policy\_document | Extensions or overrides for the IAM role assigned to EC2 instances | `string` | `"{}"` | no |
232234
| force\_destroy | Force destroy the S3 bucket for load balancer logs | `bool` | `false` | no |
233235
| health\_streaming\_delete\_on\_terminate | Whether to delete the log group when the environment is terminated. If false, the health data is kept RetentionInDays days. | `bool` | `false` | no |
234236
| health\_streaming\_enabled | For environments with enhanced health reporting enabled, whether to create a group in CloudWatch Logs for environment health and archive Elastic Beanstalk environment health data. For information about enabling enhanced health, see aws:elasticbeanstalk:healthreporting:system. | `bool` | `false` | no |
@@ -250,6 +252,7 @@ Available targets:
250252
| managed\_actions\_enabled | Enable managed platform updates. When you set this to true, you must also specify a `PreferredStartTime` and `UpdateLevel` | `bool` | `true` | no |
251253
| name | Solution name, e.g. 'app' or 'cluster' | `string` | n/a | yes |
252254
| namespace | Namespace, which could be your organization name, e.g. 'eg' or 'cp' | `string` | `""` | no |
255+
| prefer\_legacy\_ssm\_policy | Whether to use AmazonEC2RoleforSSM (will soon be deprecated) or AmazonSSMManagedInstanceCore policy | `bool` | `true` | no |
253256
| preferred\_start\_time | Configure a maintenance window for managed actions in UTC | `string` | `"Sun:10:00"` | no |
254257
| region | AWS region | `string` | n/a | yes |
255258
| rolling\_update\_enabled | Whether to enable rolling update | `bool` | `true` | no |
@@ -295,6 +298,7 @@ Available targets:
295298
| tier | The environment tier |
296299
| triggers | Autoscaling triggers in use by this environment |
297300

301+
<!-- markdownlint-restore -->
298302

299303

300304

docs/terraform.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!-- markdownlint-disable -->
12
## Requirements
23

34
| Name | Version |
@@ -52,6 +53,7 @@
5253
| env\_vars | Map of custom ENV variables to be provided to the application running on Elastic Beanstalk, e.g. env\_vars = { DB\_USER = 'admin' DB\_PASS = 'xxxxxx' } | `map(string)` | `{}` | no |
5354
| environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | `string` | `""` | no |
5455
| environment\_type | Environment type, e.g. 'LoadBalanced' or 'SingleInstance'. If setting to 'SingleInstance', `rolling_update_type` must be set to 'Time', `updating_min_in_service` must be set to 0, and `loadbalancer_subnets` will be unused (it applies to the ELB, which does not exist in SingleInstance environments) | `string` | `"LoadBalanced"` | no |
56+
| extended\_ec2\_policy\_document | Extensions or overrides for the IAM role assigned to EC2 instances | `string` | `"{}"` | no |
5557
| force\_destroy | Force destroy the S3 bucket for load balancer logs | `bool` | `false` | no |
5658
| health\_streaming\_delete\_on\_terminate | Whether to delete the log group when the environment is terminated. If false, the health data is kept RetentionInDays days. | `bool` | `false` | no |
5759
| health\_streaming\_enabled | For environments with enhanced health reporting enabled, whether to create a group in CloudWatch Logs for environment health and archive Elastic Beanstalk environment health data. For information about enabling enhanced health, see aws:elasticbeanstalk:healthreporting:system. | `bool` | `false` | no |
@@ -73,6 +75,7 @@
7375
| managed\_actions\_enabled | Enable managed platform updates. When you set this to true, you must also specify a `PreferredStartTime` and `UpdateLevel` | `bool` | `true` | no |
7476
| name | Solution name, e.g. 'app' or 'cluster' | `string` | n/a | yes |
7577
| namespace | Namespace, which could be your organization name, e.g. 'eg' or 'cp' | `string` | `""` | no |
78+
| prefer\_legacy\_ssm\_policy | Whether to use AmazonEC2RoleforSSM (will soon be deprecated) or AmazonSSMManagedInstanceCore policy | `bool` | `true` | no |
7679
| preferred\_start\_time | Configure a maintenance window for managed actions in UTC | `string` | `"Sun:10:00"` | no |
7780
| region | AWS region | `string` | n/a | yes |
7881
| rolling\_update\_enabled | Whether to enable rolling update | `bool` | `true` | no |
@@ -118,3 +121,4 @@
118121
| tier | The environment tier |
119122
| triggers | Autoscaling triggers in use by this environment |
120123

124+
<!-- markdownlint-restore -->

examples/complete/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,18 @@ module "elastic_beanstalk_environment" {
9595

9696
additional_settings = var.additional_settings
9797
env_vars = var.env_vars
98+
99+
extended_ec2_policy_document = data.aws_iam_policy_document.minimal_s3_permissions.json
100+
prefer_legacy_ssm_policy = false
101+
}
102+
103+
data "aws_iam_policy_document" "minimal_s3_permissions" {
104+
statement {
105+
sid = "AllowS3OperationsOnElasticBeanstalkBuckets"
106+
actions = [
107+
"s3:ListAllMyBuckets",
108+
"s3:GetBucketLocation"
109+
]
110+
resources = ["*"]
111+
}
98112
}

examples/complete/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = "~> 0.12.0"
2+
required_version = ">= 0.12.0"
33

44
required_providers {
55
aws = "~> 2.0"

main.tf

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ resource "aws_iam_role" "ec2" {
9191
resource "aws_iam_role_policy" "default" {
9292
name = "${module.label.id}-eb-default"
9393
role = aws_iam_role.ec2.id
94-
policy = data.aws_iam_policy_document.default.json
94+
policy = data.aws_iam_policy_document.extended.json
9595
}
9696

9797
resource "aws_iam_role_policy_attachment" "web_tier" {
@@ -106,7 +106,7 @@ resource "aws_iam_role_policy_attachment" "worker_tier" {
106106

107107
resource "aws_iam_role_policy_attachment" "ssm_ec2" {
108108
role = aws_iam_role.ec2.name
109-
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM"
109+
policy_arn = var.prefer_legacy_ssm_policy ? "arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM" : "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
110110

111111
lifecycle {
112112
create_before_destroy = true
@@ -294,6 +294,11 @@ data "aws_iam_policy_document" "default" {
294294
}
295295
}
296296

297+
data "aws_iam_policy_document" "extended" {
298+
source_json = data.aws_iam_policy_document.default.json
299+
override_json = var.extended_ec2_policy_document
300+
}
301+
297302
resource "aws_iam_instance_profile" "ec2" {
298303
name = "${module.label.id}-eb-ec2"
299304
role = aws_iam_role.ec2.name

variables.tf

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,4 +489,16 @@ variable "deployment_timeout" {
489489
type = number
490490
default = 600
491491
description = "Number of seconds to wait for an instance to complete executing commands"
492-
}
492+
}
493+
494+
variable "extended_ec2_policy_document" {
495+
type = string
496+
default = "{}"
497+
description = "Extensions or overrides for the IAM role assigned to EC2 instances"
498+
}
499+
500+
variable "prefer_legacy_ssm_policy" {
501+
type = bool
502+
default = true
503+
description = "Whether to use AmazonEC2RoleforSSM (will soon be deprecated) or AmazonSSMManagedInstanceCore policy"
504+
}

0 commit comments

Comments
 (0)