Skip to content

Commit 169f560

Browse files
author
Rajeev Jaggavarapu
committed
Inital commit for AWS Session Manager module
0 parents  commit 169f560

File tree

8 files changed

+352
-0
lines changed

8 files changed

+352
-0
lines changed

.github/workflows/checkov.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
name: Static security analysis for Terraform
3+
4+
on:
5+
push:
6+
branches:
7+
- master
8+
pull_request:
9+
branches:
10+
- master
11+
jobs:
12+
checkov-job:
13+
runs-on: ubuntu-latest
14+
name: checkov-action
15+
steps:
16+
- name: Checkout repo
17+
uses: actions/checkout@v2
18+
19+
- name: Run Checkov action
20+
id: checkov
21+
uses: bridgecrewio/checkov-action@master
22+
with:
23+
directory: ./

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.terraform
2+
*.tfstate*
3+
*.tfvars*
4+
other_vars.tf
5+
provider.tf

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Terraform AWS Session Manager Resources
2+
3+
## Use as a Module
4+
5+
```hcl
6+
module "ssm_resources" {
7+
source = "./"
8+
kms_key = {
9+
name = "ssm-cmk-key"
10+
description = "CMK for cloudwath logs and session"
11+
deletion_window_in_days = 7
12+
}
13+
cloudwatch_log_group_name = "/ssm/session-logs"
14+
enable_log_to_cloudwatch = true
15+
}
16+
```
17+
18+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
19+
## Requirements
20+
21+
| Name | Version |
22+
|------|---------|
23+
| terraform | ~> 0.12.24 |
24+
| aws | ~> 2.60 |
25+
26+
## Providers
27+
28+
| Name | Version |
29+
|------|---------|
30+
| aws | ~> 2.60 |
31+
32+
## Inputs
33+
34+
| Name | Description | Type | Default | Required |
35+
|------|-------------|------|---------|:--------:|
36+
| cloudwatch\_log\_group\_name | Name of the CloudWatch Log Group for storing SSM Session Logs | `string` | `"/ssm/session-logs"` | no |
37+
| cloudwatch\_logs\_retention | Number of days to retain Session Logs in CloudWatch | `number` | `30` | no |
38+
| create\_ssm\_document | Do you want to create SSM Document | `bool` | `true` | no |
39+
| default\_user | operating system user name for starting sessions | `string` | `"ec2-user"` | no |
40+
| enable\_log\_to\_cloudwatch | Enable Session Manager to Log to CloudWatch Logs | `bool` | `true` | no |
41+
| kms\_key | KMS Key Details | `map(string)` | <pre>{<br> "deletion_window_in_days": 7,<br> "description": "CMK for cloudwath logs and session",<br> "name": "ssm-cmk-key"<br>}</pre> | no |
42+
| run\_as\_enabled | Do you want to use Specify Operating System user for sessions | `bool` | `true` | no |
43+
| tags | A map of tags to add to all resources | `map(string)` | `{}` | no |
44+
45+
## Outputs
46+
47+
| Name | Description |
48+
|------|-------------|
49+
| cloudwatch\_log\_group\_arn | n/a |
50+
| ssm\_cloudwatch\_log\_group\_arn | The Amazon Resource Name (ARN) specifying the log group for SSM |
51+
| ssm\_kms\_key\_arn | KMS key used for SSM |
52+
| ssm\_role\_arn | n/a |
53+
54+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

data.tf

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
data "aws_caller_identity" "current" {
2+
3+
}
4+
5+
data "aws_region" "current" {
6+
7+
}
8+
9+
10+
data "aws_iam_policy_document" "kms_access" {
11+
statement {
12+
sid = "KMS Key Default"
13+
principals {
14+
type = "AWS"
15+
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
16+
}
17+
actions = [
18+
"kms:*",
19+
]
20+
21+
resources = ["*"]
22+
23+
}
24+
25+
statement {
26+
sid = "CloudWatchLogsEncryption"
27+
principals {
28+
type = "Service"
29+
identifiers = ["logs.${data.aws_region.current.name}.amazonaws.com"]
30+
}
31+
actions = [
32+
"kms:Encrypt*",
33+
"kms:Decrypt*",
34+
"kms:ReEncrypt*",
35+
"kms:GenerateDataKey*",
36+
"kms:Describe*",
37+
]
38+
39+
resources = ["*"]
40+
}
41+
42+
}
43+
44+
45+
data "aws_iam_policy" "AmazonSSMManagedInstanceCore" {
46+
arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
47+
}
48+
49+
data "aws_iam_policy" "AmazonEC2RoleforSSM" {
50+
arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM"
51+
}
52+
53+
data "aws_iam_policy_document" "ssm_s3_cwl_access" {
54+
55+
# A custom policy for CloudWatch Logs access
56+
# https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/permissions-reference-cwl.html
57+
statement {
58+
sid = "SSMAccess"
59+
60+
actions = [
61+
"ssmmessages:CreateControlChannel",
62+
"ssmmessages:CreateDataChannel",
63+
"ssmmessages:OpenControlChannel",
64+
"ssmmessages:OpenDataChannel",
65+
"ssm:UpdateInstanceInformation"
66+
]
67+
68+
resources = ["*"]
69+
}
70+
statement {
71+
sid = "CloudWatchLogsAccessForSessionManager"
72+
73+
actions = [
74+
"logs:PutLogEvents",
75+
"logs:CreateLogStream",
76+
"logs:DescribeLogGroups",
77+
"logs:DescribeLogStreams",
78+
]
79+
80+
resources = aws_cloudwatch_log_group.session_manager_log_group.*.arn
81+
}
82+
83+
statement {
84+
sid = "KMSEncryptionForSessionManager"
85+
86+
actions = [
87+
"kms:DescribeKey",
88+
"kms:GenerateDataKey",
89+
"kms:Decrypt",
90+
"kms:Encrypt",
91+
]
92+
93+
resources = ["${module.kms.arn}"]
94+
}
95+
96+
}

main.tf

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
module "kms" {
2+
source = "app.terraform.io/foss-cafe/kms/aws"
3+
version = "1.0.0"
4+
enabled = true
5+
name = lookup(var.kms_key, "name")
6+
description = lookup(var.kms_key, "description", "CMK for cloudwath logs and session")
7+
policy = data.aws_iam_policy_document.kms_access.json
8+
deletion_window_in_days = lookup(var.kms_key, "deletion_window_in_days", 7)
9+
tags = var.tags
10+
11+
}
12+
13+
resource "aws_cloudwatch_log_group" "session_manager_log_group" {
14+
count = var.enable_log_to_cloudwatch ? 1 : 0
15+
name = var.cloudwatch_log_group_name
16+
retention_in_days = var.cloudwatch_logs_retention
17+
kms_key_id = module.kms.arn
18+
19+
tags = var.tags
20+
}
21+
#### This Document is not working as expected need rework
22+
# resource "aws_ssm_document" "session_manager_prefs" {
23+
# count = var.create_ssm_document ? 1: 0
24+
# name = "SSM-SessionManagerRunShell"
25+
# document_type = "Session"
26+
# document_format = "JSON"
27+
# tags = var.tags
28+
29+
# content = <<DOC
30+
# {
31+
# "schemaVersion": "1.0",
32+
# "description": "Document to hold regional settings for Session Manager",
33+
# "sessionType": "Standard_Stream",
34+
# "inputs": {
35+
# "s3BucketName": "",
36+
# "s3KeyPrefix": "",
37+
# "s3EncryptionEnabled": true,
38+
# "cloudWatchLogGroupName": "${var.enable_log_to_cloudwatch ? var.cloudwatch_log_group_name : ""}",
39+
# "cloudWatchEncryptionEnabled": "${var.enable_log_to_cloudwatch ? "true" : "false"}",
40+
# "kmsKeyId": "${module.kms.key_id}",
41+
# "runAsEnabled": "false",
42+
# "runAsDefaultUser": ""
43+
# }
44+
# }
45+
# DOC
46+
# }
47+
48+
49+
# Create EC2 Instance Role for SSM
50+
resource "aws_iam_role" "ssm_role" {
51+
name = "SessionManagerRole"
52+
path = "/"
53+
tags = var.tags
54+
55+
assume_role_policy = <<EOF
56+
{
57+
"Version": "2012-10-17",
58+
"Statement": [
59+
{
60+
"Action": "sts:AssumeRole",
61+
"Principal": {
62+
"Service": "ec2.amazonaws.com"
63+
},
64+
"Effect": "Allow",
65+
"Sid": ""
66+
}
67+
]
68+
}
69+
EOF
70+
}
71+
72+
73+
74+
resource "aws_iam_policy" "ssm_s3_cwl_access" {
75+
name = "SessionManagerPermissions"
76+
path = "/"
77+
policy = data.aws_iam_policy_document.ssm_s3_cwl_access.json
78+
}
79+
80+
resource "aws_iam_role_policy_attachment" "SSM_role_managed_instace_policy_attach" {
81+
role = aws_iam_role.ssm_role.name
82+
policy_arn = data.aws_iam_policy.AmazonSSMManagedInstanceCore.arn
83+
}
84+
85+
resource "aws_iam_role_policy_attachment" "SSM_role_for_ec2_policy_attach" {
86+
role = aws_iam_role.ssm_role.name
87+
policy_arn = data.aws_iam_policy.AmazonEC2RoleforSSM.arn
88+
}
89+
90+
resource "aws_iam_role_policy_attachment" "SSM_s3_cwl_policy_attach" {
91+
role = aws_iam_role.ssm_role.name
92+
policy_arn = aws_iam_policy.ssm_s3_cwl_access.arn
93+
}
94+
95+
resource "aws_iam_instance_profile" "ssm_profile" {
96+
name = "ssm_profile"
97+
role = aws_iam_role.ssm_role.name
98+
}

outputs.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
output "cloudwatch_log_group_arn" {
3+
value = aws_cloudwatch_log_group.session_manager_log_group.arn
4+
}
5+
6+
output "ssm_kms_key_arn" {
7+
description = "KMS key used for SSM"
8+
value = module.kms.arn
9+
}
10+
11+
output "ssm_cloudwatch_log_group_arn" {
12+
description = "The Amazon Resource Name (ARN) specifying the log group for SSM"
13+
value = join("", aws_cloudwatch_log_group.session_manager_log_group.*.arn)
14+
}
15+
16+
output "ssm_role_arn" {
17+
value = aws_iam_role.ssm_role.arn
18+
}

variables.tf

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
variable "kms_key" {
2+
type = map(string)
3+
description = "KMS Key Details"
4+
default = {
5+
name = "ssm-cmk-key"
6+
description = "CMK for cloudwath logs and session"
7+
deletion_window_in_days = 7
8+
}
9+
}
10+
11+
variable "cloudwatch_logs_retention" {
12+
description = "Number of days to retain Session Logs in CloudWatch"
13+
type = number
14+
default = 30
15+
}
16+
17+
variable "cloudwatch_log_group_name" {
18+
description = "Name of the CloudWatch Log Group for storing SSM Session Logs"
19+
type = string
20+
default = "/ssm/session-logs"
21+
}
22+
23+
variable "tags" {
24+
description = "A map of tags to add to all resources"
25+
type = map(string)
26+
default = {}
27+
}
28+
29+
variable "enable_log_to_cloudwatch" {
30+
description = "Enable Session Manager to Log to CloudWatch Logs"
31+
type = bool
32+
default = true
33+
}
34+
35+
variable "run_as_enabled" {
36+
type = bool
37+
description = "Do you want to use Specify Operating System user for sessions"
38+
default = true
39+
}
40+
41+
variable "default_user" {
42+
type = string
43+
description = "operating system user name for starting sessions"
44+
default = "ec2-user"
45+
}
46+
47+
variable "create_ssm_document" {
48+
type = bool
49+
description = "Do you want to create SSM Document"
50+
default = true
51+
}

versions.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
terraform {
2+
required_version = "~> 0.12.24"
3+
4+
required_providers {
5+
aws = "~> 2.60"
6+
}
7+
}

0 commit comments

Comments
 (0)