├── .github └── workflows │ ├── json.yml │ ├── markdown.yml │ ├── terraform.yml │ └── yaml.yml ├── .gitignore ├── .markdownlint.json ├── .prettierignore ├── .yamllint ├── LICENSE ├── Makefile ├── README.md ├── VERSION ├── examples ├── complete │ ├── main.tf │ ├── outputs.tf │ ├── providers.tf │ └── user_data.sh └── minimal │ ├── main.tf │ ├── outputs.tf │ └── providers.tf ├── main.tf ├── modules └── .gitkeep ├── outputs.tf ├── variables.tf └── versions.tf /.github/workflows/json.yml: -------------------------------------------------------------------------------- 1 | name: JSON 2 | on: 3 | pull_request: 4 | 5 | jobs: 6 | format: 7 | name: Format 8 | runs-on: ubuntu-latest 9 | timeout-minutes: 5 10 | steps: 11 | - name: Checkout 12 | uses: actions/checkout@v2.1.0 13 | - name: Prettier 14 | run: docker run --rm -v ${PWD}:/work tmknom/prettier --list-different --parser=json '**/*.json' 15 | -------------------------------------------------------------------------------- /.github/workflows/markdown.yml: -------------------------------------------------------------------------------- 1 | name: Markdown 2 | on: 3 | pull_request: 4 | 5 | jobs: 6 | lint: 7 | name: Lint 8 | runs-on: ubuntu-latest 9 | timeout-minutes: 5 10 | steps: 11 | - name: Checkout 12 | uses: actions/checkout@v2.1.0 13 | - name: markdownlint 14 | run: docker run --rm -i -v ${PWD}:/work tmknom/markdownlint 15 | 16 | format: 17 | name: Format 18 | runs-on: ubuntu-latest 19 | timeout-minutes: 5 20 | steps: 21 | - name: Checkout 22 | uses: actions/checkout@v2.1.0 23 | - name: Prettier 24 | run: docker run --rm -v ${PWD}:/work tmknom/prettier --list-different --parser=markdown '**/*.md' 25 | -------------------------------------------------------------------------------- /.github/workflows/terraform.yml: -------------------------------------------------------------------------------- 1 | name: Terraform 2 | on: 3 | pull_request: 4 | 5 | env: 6 | TERRAFORM_VERSION: 0.12.24 7 | AWS_DEFAULT_REGION: us-east-1 8 | 9 | jobs: 10 | format: 11 | name: Format 12 | runs-on: ubuntu-latest 13 | timeout-minutes: 5 14 | steps: 15 | - name: Checkout 16 | uses: actions/checkout@v2.1.0 17 | - name: Format all 18 | uses: hashicorp/terraform-github-actions@v0.8.0 19 | env: 20 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 21 | with: 22 | tf_actions_version: ${{ env.TERRAFORM_VERSION }} 23 | tf_actions_comment: true 24 | tf_actions_subcommand: fmt 25 | 26 | validate: 27 | name: Validate 28 | runs-on: ubuntu-latest 29 | timeout-minutes: 5 30 | strategy: 31 | matrix: 32 | dir: 33 | - . 34 | - examples/minimal 35 | - examples/complete 36 | steps: 37 | - name: Checkout 38 | uses: actions/checkout@v2.1.0 39 | - name: Init 40 | uses: hashicorp/terraform-github-actions@v0.8.0 41 | env: 42 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 43 | with: 44 | tf_actions_version: ${{ env.TERRAFORM_VERSION }} 45 | tf_actions_working_dir: ${{ matrix.dir }} 46 | tf_actions_comment: true 47 | tf_actions_subcommand: init 48 | - name: Validate 49 | uses: hashicorp/terraform-github-actions@v0.8.0 50 | env: 51 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 52 | with: 53 | tf_actions_version: ${{ env.TERRAFORM_VERSION }} 54 | tf_actions_working_dir: ${{ matrix.dir }} 55 | tf_actions_comment: true 56 | tf_actions_subcommand: validate 57 | 58 | lint: 59 | name: Lint 60 | runs-on: ubuntu-latest 61 | timeout-minutes: 5 62 | steps: 63 | - name: Checkout 64 | uses: actions/checkout@v2.1.0 65 | - name: TFLint 66 | run: docker run --rm -v ${PWD}:/data wata727/tflint 67 | -------------------------------------------------------------------------------- /.github/workflows/yaml.yml: -------------------------------------------------------------------------------- 1 | name: YAML 2 | on: 3 | pull_request: 4 | 5 | jobs: 6 | lint: 7 | name: Lint 8 | runs-on: ubuntu-latest 9 | timeout-minutes: 5 10 | steps: 11 | - name: Checkout 12 | uses: actions/checkout@v2.1.0 13 | - name: yamllint 14 | run: docker run --rm -v ${PWD}:/work tmknom/yamllint --strict . 15 | 16 | format: 17 | name: Format 18 | runs-on: ubuntu-latest 19 | timeout-minutes: 5 20 | steps: 21 | - name: Checkout 22 | uses: actions/checkout@v2.1.0 23 | - name: Prettier 24 | run: docker run --rm -v ${PWD}:/work tmknom/prettier --list-different --parser=yaml '**/*.y*ml' 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Local .terraform directories 2 | **/.terraform/* 3 | 4 | # .tfstate files 5 | *.tfstate 6 | *.tfstate.* 7 | 8 | # .tfvars files 9 | *.tfvars 10 | 11 | # IntelliJ 12 | .idea/ 13 | 14 | # direnv 15 | .envrc 16 | 17 | # Auto include makefiles 18 | .Makefile.terraform 19 | -------------------------------------------------------------------------------- /.markdownlint.json: -------------------------------------------------------------------------------- 1 | { 2 | "line-length": false 3 | } 4 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | # https://prettier.io/docs/en/cli.html#ignore-path 2 | 3 | **/.terraform/* 4 | -------------------------------------------------------------------------------- /.yamllint: -------------------------------------------------------------------------------- 1 | extends: default 2 | 3 | rules: 4 | document-start: disable 5 | truthy: 6 | allowed-values: ['true', 'false', 'on'] 7 | line-length: 8 | max: 120 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # https://github.com/tmknom/template-terraform-module 2 | TERRAFORM_VERSION := 0.12.24 3 | -include .Makefile.terraform 4 | 5 | .Makefile.terraform: 6 | curl -sSL https://raw.githubusercontent.com/tmknom/template-terraform-module/0.2.7/Makefile.terraform -o .Makefile.terraform 7 | 8 | MINIMAL_DIR := ./examples/minimal 9 | COMPLETE_DIR := ./examples/complete 10 | 11 | define start_session_to_example 12 | query='Reservations[0].Instances[0].InstanceId' && \ 13 | filters='Name=tag:Name,Values=example,Name=instance-state-name,Values=running' && \ 14 | document_name='SSM-SessionManagerRunShell-for-example' && \ 15 | target=$$(aws ec2 describe-instances --output text --query $${query} --filters $${filters}) && \ 16 | exec aws ssm start-session --target $${target} --document-name $${document_name} 17 | endef 18 | 19 | start-session: ## Start session to example 20 | $(call start_session_to_example) 21 | 22 | plan-minimal: ## Run terraform plan examples/minimal 23 | $(call terraform,${MINIMAL_DIR},init,) 24 | $(call terraform,${MINIMAL_DIR},plan,) 25 | 26 | apply-minimal: ## Run terraform apply examples/minimal 27 | $(call terraform,${MINIMAL_DIR},apply,) 28 | 29 | destroy-minimal: ## Run terraform destroy examples/minimal 30 | $(call terraform,${MINIMAL_DIR},destroy,) 31 | 32 | plan-complete: ## Run terraform plan examples/complete 33 | $(call terraform,${COMPLETE_DIR},init,) 34 | $(call terraform,${COMPLETE_DIR},plan,) 35 | 36 | apply-complete: ## Run terraform apply examples/complete 37 | $(call terraform,${COMPLETE_DIR},apply,) 38 | 39 | destroy-complete: ## Run terraform destroy examples/complete 40 | $(call terraform,${COMPLETE_DIR},destroy,) 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # terraform-aws-session-manager 2 | 3 | [![Terraform Actions Status](https://github.com/tmknom/terraform-aws-session-manager/workflows/Terraform/badge.svg)](https://github.com/tmknom/terraform-aws-session-manager/actions?query=workflow%3ATerraform) 4 | [![Markdown Actions Status](https://github.com/tmknom/terraform-aws-session-manager/workflows/Markdown/badge.svg)](https://github.com/tmknom/terraform-aws-session-manager/actions?query=workflow%3AMarkdown) 5 | [![YAML Actions Status](https://github.com/tmknom/terraform-aws-session-manager/workflows/YAML/badge.svg)](https://github.com/tmknom/terraform-aws-session-manager/actions?query=workflow%3AYAML) 6 | [![JSON Actions Status](https://github.com/tmknom/terraform-aws-session-manager/workflows/JSON/badge.svg)](https://github.com/tmknom/terraform-aws-session-manager/actions?query=workflow%3AJSON) 7 | [![GitHub tag](https://img.shields.io/github/tag/tmknom/terraform-aws-session-manager.svg)](https://registry.terraform.io/modules/tmknom/session-manager/aws) 8 | [![License](https://img.shields.io/github/license/tmknom/terraform-aws-session-manager.svg)](https://opensource.org/licenses/Apache-2.0) 9 | 10 | Terraform module which creates Session Manager resources on AWS. 11 | 12 | ## Description 13 | 14 | Provision [SSM Documents](https://docs.aws.amazon.com/systems-manager/latest/userguide/getting-started-configure-preferences-cli.html), 15 | [EC2 Instances](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html) and 16 | [Instance Profiles for Session Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-getting-started-instance-profile.html). 17 | 18 | This module provides recommended settings: 19 | 20 | - No open inbound ports 21 | - Loggable session activity 22 | 23 | ## Usage 24 | 25 | ### Minimal 26 | 27 | ```hcl 28 | module "session_manager" { 29 | source = "git::https://github.com/tmknom/terraform-aws-session-manager.git?ref=tags/2.0.0" 30 | name = "example" 31 | instance_type = "t2.micro" 32 | subnet_id = var.subnet_id 33 | vpc_id = var.vpc_id 34 | } 35 | ``` 36 | 37 | ### Complete 38 | 39 | ```hcl 40 | module "session_manager" { 41 | source = "git::https://github.com/tmknom/terraform-aws-session-manager.git?ref=tags/2.0.0" 42 | name = "example" 43 | instance_type = "t2.micro" 44 | subnet_id = var.subnet_id 45 | vpc_id = var.vpc_id 46 | 47 | ssm_document_name = "SSM-SessionManagerRunShell-for-example" 48 | s3_bucket_name = var.s3_bucket_name 49 | s3_key_prefix = "prefix" 50 | s3_encryption_enabled = false 51 | cloudwatch_log_group_name = var.cloudwatch_log_group_name 52 | cloudwatch_encryption_enabled = false 53 | ami = var.ami 54 | vpc_security_group_ids = var.vpc_security_group_ids 55 | iam_policy = var.iam_policy 56 | iam_path = "/service-role/" 57 | description = "This is example" 58 | 59 | tags = { 60 | Environment = "prod" 61 | } 62 | } 63 | ``` 64 | 65 | ## Examples 66 | 67 | - [Minimal](https://github.com/tmknom/terraform-aws-session-manager/tree/master/examples/minimal) 68 | - [Complete](https://github.com/tmknom/terraform-aws-session-manager/tree/master/examples/complete) 69 | 70 | 71 | 72 | ## Requirements 73 | 74 | | Name | Version | 75 | | --------- | ------- | 76 | | terraform | >= 0.12 | 77 | 78 | ## Providers 79 | 80 | | Name | Version | 81 | | ---- | ------- | 82 | | aws | n/a | 83 | 84 | ## Inputs 85 | 86 | | Name | Description | Type | Default | Required | 87 | | ----------------------------- | --------------------------------------------------------------------- | -------------- | ------------------------------ | :------: | 88 | | instance_type | The type of instance to start. | `string` | n/a | yes | 89 | | name | The name of the Session Manager. | `string` | n/a | yes | 90 | | subnet_id | The VPC Subnet ID to launch in. | `string` | n/a | yes | 91 | | vpc_id | The VPC ID. | `string` | n/a | yes | 92 | | ami | The AMI to use for the instance. | `string` | `""` | no | 93 | | cloudwatch_encryption_enabled | Specify true to indicate that encryption for CloudWatch Logs enabled. | `bool` | `true` | no | 94 | | cloudwatch_log_group_name | The name of the log group. | `string` | `""` | no | 95 | | description | The description of the all resources. | `string` | `"Managed by Terraform"` | no | 96 | | iam_path | Path in which to create the IAM Role and the IAM Policy. | `string` | `"/"` | no | 97 | | iam_policy | The policy document. This is a JSON formatted string. | `string` | `""` | no | 98 | | s3_bucket_name | The name of the bucket. | `string` | `""` | no | 99 | | s3_encryption_enabled | Specify true to indicate that encryption for S3 Bucket enabled. | `bool` | `true` | no | 100 | | s3_key_prefix | The prefix for the specified S3 bucket. | `string` | `""` | no | 101 | | ssm_document_name | The name of the document. | `string` | `"SSM-SessionManagerRunShell"` | no | 102 | | tags | A mapping of tags to assign to all resources. | `map(string)` | `{}` | no | 103 | | user_data | The user data to provide when launching the instance. | `string` | `""` | no | 104 | | vpc_security_group_ids | A list of security group IDs to associate with. | `list(string)` | `[]` | no | 105 | 106 | ## Outputs 107 | 108 | | Name | Description | 109 | | ------------------------------------- | ------------------------------------------------------------ | 110 | | iam_instance_profile_arn | The ARN assigned by AWS to the instance profile. | 111 | | iam_instance_profile_create_date | The creation timestamp of the instance profile. | 112 | | iam_instance_profile_id | The instance profile's ID. | 113 | | iam_instance_profile_name | The instance profile's name. | 114 | | iam_instance_profile_path | The path of the instance profile in IAM. | 115 | | iam_instance_profile_role | The role assigned to the instance profile. | 116 | | iam_instance_profile_unique_id | The unique ID assigned by AWS. | 117 | | iam_policy_arn | The ARN assigned by AWS to this IAM Policy. | 118 | | iam_policy_description | The description of the IAM Policy. | 119 | | iam_policy_document | The policy document of the IAM Policy. | 120 | | iam_policy_id | The IAM Policy's ID. | 121 | | iam_policy_name | The name of the IAM Policy. | 122 | | iam_policy_path | The path of the IAM Policy. | 123 | | iam_role_arn | The Amazon Resource Name (ARN) specifying the IAM Role. | 124 | | iam_role_create_date | The creation date of the IAM Role. | 125 | | iam_role_description | The description of the IAM Role. | 126 | | iam_role_name | The name of the IAM Role. | 127 | | iam_role_unique_id | The stable and unique string identifying the IAM Role. | 128 | | instance_arn | The ARN of the instance. | 129 | | instance_availability_zone | The availability zone of the instance. | 130 | | instance_id | The instance ID. | 131 | | instance_key_name | The key name of the instance. | 132 | | instance_placement_group | The placement group of the instance. | 133 | | instance_primary_network_interface_id | The ID of the instance's primary network interface. | 134 | | instance_private_dns | The private DNS name assigned to the instance. | 135 | | instance_private_ip | The private IP address assigned to the instance. | 136 | | instance_security_groups | The associated security groups. | 137 | | instance_subnet_id | The VPC subnet ID. | 138 | | security_group_arn | The ARN of the security group. | 139 | | security_group_description | The description of the security group. | 140 | | security_group_egress | The egress rules of the security group. | 141 | | security_group_id | The ID of the security group. | 142 | | security_group_ingress | The ingress rules of the security group. | 143 | | security_group_name | The name of the security group. | 144 | | security_group_owner_id | The owner ID of the security group. | 145 | | security_group_vpc_id | The VPC ID of the security group. | 146 | | ssm_document_default_version | The default version of the document. | 147 | | ssm_document_description | The description of the document. | 148 | | ssm_document_hash | The sha1 or sha256 of the document content. | 149 | | ssm_document_hash_type | The hashing algorithm used when hashing the content. | 150 | | ssm_document_latest_version | The latest version of the document. | 151 | | ssm_document_owner | The AWS user account of the person who created the document. | 152 | | ssm_document_parameter | The parameters that are available to this document. | 153 | | ssm_document_platform_types | A list of OS platforms compatible with this SSM document. | 154 | | ssm_document_schema_version | The schema version of the document. | 155 | | ssm_document_status | The current status of the document. | 156 | 157 | 158 | 159 | ## Development 160 | 161 | ### Development Requirements 162 | 163 | - [Docker](https://www.docker.com/) 164 | 165 | ### Configure environment variables 166 | 167 | ```shell 168 | export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE 169 | export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY 170 | export AWS_DEFAULT_REGION=ap-northeast-1 171 | ``` 172 | 173 | ### Installation 174 | 175 | ```shell 176 | git clone git@github.com:tmknom/terraform-aws-session-manager.git 177 | cd terraform-aws-session-manager 178 | make install 179 | ``` 180 | 181 | ### Makefile targets 182 | 183 | ```text 184 | apply-complete Run terraform apply examples/complete 185 | apply-minimal Run terraform apply examples/minimal 186 | check-format Check format code 187 | clean Clean .terraform 188 | destroy-complete Run terraform destroy examples/complete 189 | destroy-minimal Run terraform destroy examples/minimal 190 | diff Word diff 191 | docs Generate docs 192 | format Format code 193 | help Show help 194 | install Install requirements 195 | lint Lint code 196 | plan-complete Run terraform plan examples/complete 197 | plan-minimal Run terraform plan examples/minimal 198 | release Release GitHub and Terraform Module Registry 199 | start-session Start session to example 200 | upgrade Upgrade makefile 201 | ``` 202 | 203 | ### Releasing new versions 204 | 205 | Bump VERSION file, and run `make release`. 206 | 207 | ### Terraform Module Registry 208 | 209 | - 210 | 211 | ## License 212 | 213 | Apache 2 Licensed. See LICENSE for full details. 214 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 2.0.0 2 | -------------------------------------------------------------------------------- /examples/complete/main.tf: -------------------------------------------------------------------------------- 1 | module "session_manager" { 2 | source = "../../" 3 | name = "example" 4 | instance_type = "t2.micro" 5 | subnet_id = module.vpc.public_subnet_ids[0] 6 | vpc_id = module.vpc.vpc_id 7 | 8 | ssm_document_name = "SSM-SessionManagerRunShell-for-example" 9 | s3_bucket_name = aws_s3_bucket.default.id 10 | s3_key_prefix = "prefix" 11 | s3_encryption_enabled = false 12 | cloudwatch_log_group_name = aws_cloudwatch_log_group.default.name 13 | cloudwatch_encryption_enabled = false 14 | ami = data.aws_ami.default.id 15 | vpc_security_group_ids = [aws_security_group.default.id] 16 | user_data = file("${path.module}/user_data.sh") 17 | iam_policy = data.aws_iam_policy_document.default.json 18 | iam_path = "/service-role/" 19 | description = "This is example" 20 | 21 | tags = { 22 | Environment = "prod" 23 | } 24 | } 25 | 26 | resource "aws_s3_bucket" "default" { 27 | bucket = "session-manager-${data.aws_caller_identity.current.account_id}" 28 | acl = "private" 29 | force_destroy = true 30 | } 31 | 32 | resource "aws_cloudwatch_log_group" "default" { 33 | name = "/session-manager/example" 34 | retention_in_days = "1" 35 | } 36 | 37 | data "aws_ami" "default" { 38 | most_recent = true 39 | owners = ["amazon"] 40 | 41 | filter { 42 | name = "owner-alias" 43 | values = ["amazon"] 44 | } 45 | 46 | filter { 47 | name = "name" 48 | values = ["amzn2-ami-hvm-2.0.????????-x86_64-gp2"] 49 | } 50 | 51 | filter { 52 | name = "state" 53 | values = ["available"] 54 | } 55 | } 56 | 57 | resource "aws_security_group" "default" { 58 | name = "session-manager" 59 | vpc_id = module.vpc.vpc_id 60 | } 61 | 62 | resource "aws_security_group_rule" "ingress" { 63 | type = "ingress" 64 | from_port = 0 65 | to_port = 0 66 | protocol = "-1" 67 | cidr_blocks = ["0.0.0.0/0"] 68 | security_group_id = aws_security_group.default.id 69 | } 70 | 71 | data "aws_iam_policy_document" "default" { 72 | source_json = data.aws_iam_policy.default.policy 73 | 74 | # A custom policy for S3 bucket access 75 | # https://docs.aws.amazon.com/en_us/systems-manager/latest/userguide/setup-instance-profile.html#instance-profile-custom-s3-policy 76 | statement { 77 | sid = "S3BucketAccessForSessionManager" 78 | 79 | actions = [ 80 | "s3:PutObject", 81 | ] 82 | 83 | resources = [ 84 | "${aws_s3_bucket.default.arn}/*", 85 | ] 86 | } 87 | 88 | # A custom policy for CloudWatch Logs access 89 | # https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/permissions-reference-cwl.html 90 | statement { 91 | sid = "CloudWatchLogsAccessForSessionManager" 92 | 93 | actions = [ 94 | "logs:PutLogEvents", 95 | "logs:CreateLogStream", 96 | ] 97 | 98 | resources = ["*"] 99 | } 100 | } 101 | 102 | data "aws_iam_policy" "default" { 103 | arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" 104 | } 105 | 106 | module "vpc" { 107 | source = "git::https://github.com/tmknom/terraform-aws-vpc.git?ref=tags/2.0.1" 108 | cidr_block = local.cidr_block 109 | name = "session-manager" 110 | public_subnet_cidr_blocks = [cidrsubnet(local.cidr_block, 8, 0), cidrsubnet(local.cidr_block, 8, 1)] 111 | public_availability_zones = data.aws_availability_zones.available.names 112 | } 113 | 114 | locals { 115 | cidr_block = "10.255.0.0/16" 116 | } 117 | 118 | data "aws_availability_zones" "available" {} 119 | 120 | data "aws_caller_identity" "current" {} 121 | -------------------------------------------------------------------------------- /examples/complete/outputs.tf: -------------------------------------------------------------------------------- 1 | output "ssm_document_description" { 2 | value = module.session_manager.ssm_document_description 3 | } 4 | 5 | output "ssm_document_schema_version" { 6 | value = module.session_manager.ssm_document_schema_version 7 | } 8 | 9 | output "ssm_document_default_version" { 10 | value = module.session_manager.ssm_document_default_version 11 | } 12 | 13 | output "ssm_document_hash" { 14 | value = module.session_manager.ssm_document_hash 15 | } 16 | 17 | output "ssm_document_hash_type" { 18 | value = module.session_manager.ssm_document_hash_type 19 | } 20 | 21 | output "ssm_document_latest_version" { 22 | value = module.session_manager.ssm_document_latest_version 23 | } 24 | 25 | output "ssm_document_owner" { 26 | value = module.session_manager.ssm_document_owner 27 | } 28 | 29 | output "ssm_document_status" { 30 | value = module.session_manager.ssm_document_status 31 | } 32 | 33 | output "ssm_document_parameter" { 34 | value = module.session_manager.ssm_document_parameter 35 | } 36 | 37 | output "ssm_document_platform_types" { 38 | value = module.session_manager.ssm_document_platform_types 39 | } 40 | 41 | output "instance_id" { 42 | value = module.session_manager.instance_id 43 | } 44 | 45 | output "instance_arn" { 46 | value = module.session_manager.instance_arn 47 | } 48 | 49 | output "instance_availability_zone" { 50 | value = module.session_manager.instance_availability_zone 51 | } 52 | 53 | output "instance_placement_group" { 54 | value = module.session_manager.instance_placement_group 55 | } 56 | 57 | output "instance_key_name" { 58 | value = module.session_manager.instance_key_name 59 | } 60 | 61 | output "instance_primary_network_interface_id" { 62 | value = module.session_manager.instance_primary_network_interface_id 63 | } 64 | 65 | output "instance_private_dns" { 66 | value = module.session_manager.instance_private_dns 67 | } 68 | 69 | output "instance_private_ip" { 70 | value = module.session_manager.instance_private_ip 71 | } 72 | 73 | output "instance_security_groups" { 74 | value = module.session_manager.instance_security_groups 75 | } 76 | 77 | output "instance_subnet_id" { 78 | value = module.session_manager.instance_subnet_id 79 | } 80 | 81 | output "security_group_id" { 82 | value = module.session_manager.security_group_id 83 | } 84 | 85 | output "security_group_arn" { 86 | value = module.session_manager.security_group_arn 87 | } 88 | 89 | output "security_group_vpc_id" { 90 | value = module.session_manager.security_group_vpc_id 91 | } 92 | 93 | output "security_group_owner_id" { 94 | value = module.session_manager.security_group_owner_id 95 | } 96 | 97 | output "security_group_name" { 98 | value = module.session_manager.security_group_name 99 | } 100 | 101 | output "security_group_description" { 102 | value = module.session_manager.security_group_description 103 | } 104 | 105 | output "security_group_ingress" { 106 | value = module.session_manager.security_group_ingress 107 | } 108 | 109 | output "security_group_egress" { 110 | value = module.session_manager.security_group_egress 111 | } 112 | 113 | output "iam_instance_profile_id" { 114 | value = module.session_manager.iam_instance_profile_id 115 | } 116 | 117 | output "iam_instance_profile_arn" { 118 | value = module.session_manager.iam_instance_profile_arn 119 | } 120 | 121 | output "iam_instance_profile_create_date" { 122 | value = module.session_manager.iam_instance_profile_create_date 123 | } 124 | 125 | output "iam_instance_profile_name" { 126 | value = module.session_manager.iam_instance_profile_name 127 | } 128 | 129 | output "iam_instance_profile_path" { 130 | value = module.session_manager.iam_instance_profile_path 131 | } 132 | 133 | output "iam_instance_profile_role" { 134 | value = module.session_manager.iam_instance_profile_role 135 | } 136 | 137 | output "iam_instance_profile_unique_id" { 138 | value = module.session_manager.iam_instance_profile_unique_id 139 | } 140 | 141 | output "iam_role_arn" { 142 | value = module.session_manager.iam_role_arn 143 | } 144 | 145 | output "iam_role_create_date" { 146 | value = module.session_manager.iam_role_create_date 147 | } 148 | 149 | output "iam_role_unique_id" { 150 | value = module.session_manager.iam_role_unique_id 151 | } 152 | 153 | output "iam_role_name" { 154 | value = module.session_manager.iam_role_name 155 | } 156 | 157 | output "iam_role_description" { 158 | value = module.session_manager.iam_role_description 159 | } 160 | 161 | output "iam_policy_id" { 162 | value = module.session_manager.iam_policy_id 163 | } 164 | 165 | output "iam_policy_arn" { 166 | value = module.session_manager.iam_policy_arn 167 | } 168 | 169 | output "iam_policy_description" { 170 | value = module.session_manager.iam_policy_description 171 | } 172 | 173 | output "iam_policy_name" { 174 | value = module.session_manager.iam_policy_name 175 | } 176 | 177 | output "iam_policy_path" { 178 | value = module.session_manager.iam_policy_path 179 | } 180 | 181 | output "iam_policy_document" { 182 | value = module.session_manager.iam_policy_document 183 | } 184 | -------------------------------------------------------------------------------- /examples/complete/providers.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "ap-northeast-1" 3 | } 4 | -------------------------------------------------------------------------------- /examples/complete/user_data.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Install docker 4 | amazon-linux-extras install -y docker 5 | systemctl start docker 6 | systemctl enable docker 7 | 8 | # Install docker-compose 9 | DOCKER_COMPOSE_VERSION=1.23.2 10 | curl -L "https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose 11 | chmod +x /usr/local/bin/docker-compose 12 | -------------------------------------------------------------------------------- /examples/minimal/main.tf: -------------------------------------------------------------------------------- 1 | module "session_manager" { 2 | source = "../../" 3 | name = "example" 4 | instance_type = "t2.micro" 5 | subnet_id = module.vpc.public_subnet_ids[0] 6 | vpc_id = module.vpc.vpc_id 7 | 8 | ssm_document_name = "SSM-SessionManagerRunShell-for-example" 9 | } 10 | 11 | module "vpc" { 12 | source = "git::https://github.com/tmknom/terraform-aws-vpc.git?ref=tags/2.0.1" 13 | cidr_block = local.cidr_block 14 | name = "session-manager" 15 | public_subnet_cidr_blocks = [cidrsubnet(local.cidr_block, 8, 0), cidrsubnet(local.cidr_block, 8, 1)] 16 | public_availability_zones = data.aws_availability_zones.available.names 17 | } 18 | 19 | locals { 20 | cidr_block = "10.255.0.0/16" 21 | } 22 | 23 | data "aws_availability_zones" "available" {} 24 | -------------------------------------------------------------------------------- /examples/minimal/outputs.tf: -------------------------------------------------------------------------------- 1 | output "ssm_document_description" { 2 | value = module.session_manager.ssm_document_description 3 | } 4 | 5 | output "ssm_document_schema_version" { 6 | value = module.session_manager.ssm_document_schema_version 7 | } 8 | 9 | output "ssm_document_default_version" { 10 | value = module.session_manager.ssm_document_default_version 11 | } 12 | 13 | output "ssm_document_hash" { 14 | value = module.session_manager.ssm_document_hash 15 | } 16 | 17 | output "ssm_document_hash_type" { 18 | value = module.session_manager.ssm_document_hash_type 19 | } 20 | 21 | output "ssm_document_latest_version" { 22 | value = module.session_manager.ssm_document_latest_version 23 | } 24 | 25 | output "ssm_document_owner" { 26 | value = module.session_manager.ssm_document_owner 27 | } 28 | 29 | output "ssm_document_status" { 30 | value = module.session_manager.ssm_document_status 31 | } 32 | 33 | output "ssm_document_parameter" { 34 | value = module.session_manager.ssm_document_parameter 35 | } 36 | 37 | output "ssm_document_platform_types" { 38 | value = module.session_manager.ssm_document_platform_types 39 | } 40 | 41 | output "instance_id" { 42 | value = module.session_manager.instance_id 43 | } 44 | 45 | output "instance_arn" { 46 | value = module.session_manager.instance_arn 47 | } 48 | 49 | output "instance_availability_zone" { 50 | value = module.session_manager.instance_availability_zone 51 | } 52 | 53 | output "instance_placement_group" { 54 | value = module.session_manager.instance_placement_group 55 | } 56 | 57 | output "instance_key_name" { 58 | value = module.session_manager.instance_key_name 59 | } 60 | 61 | output "instance_primary_network_interface_id" { 62 | value = module.session_manager.instance_primary_network_interface_id 63 | } 64 | 65 | output "instance_private_dns" { 66 | value = module.session_manager.instance_private_dns 67 | } 68 | 69 | output "instance_private_ip" { 70 | value = module.session_manager.instance_private_ip 71 | } 72 | 73 | output "instance_security_groups" { 74 | value = module.session_manager.instance_security_groups 75 | } 76 | 77 | output "instance_subnet_id" { 78 | value = module.session_manager.instance_subnet_id 79 | } 80 | 81 | output "security_group_id" { 82 | value = module.session_manager.security_group_id 83 | } 84 | 85 | output "security_group_arn" { 86 | value = module.session_manager.security_group_arn 87 | } 88 | 89 | output "security_group_vpc_id" { 90 | value = module.session_manager.security_group_vpc_id 91 | } 92 | 93 | output "security_group_owner_id" { 94 | value = module.session_manager.security_group_owner_id 95 | } 96 | 97 | output "security_group_name" { 98 | value = module.session_manager.security_group_name 99 | } 100 | 101 | output "security_group_description" { 102 | value = module.session_manager.security_group_description 103 | } 104 | 105 | output "security_group_ingress" { 106 | value = module.session_manager.security_group_ingress 107 | } 108 | 109 | output "security_group_egress" { 110 | value = module.session_manager.security_group_egress 111 | } 112 | 113 | output "iam_instance_profile_id" { 114 | value = module.session_manager.iam_instance_profile_id 115 | } 116 | 117 | output "iam_instance_profile_arn" { 118 | value = module.session_manager.iam_instance_profile_arn 119 | } 120 | 121 | output "iam_instance_profile_create_date" { 122 | value = module.session_manager.iam_instance_profile_create_date 123 | } 124 | 125 | output "iam_instance_profile_name" { 126 | value = module.session_manager.iam_instance_profile_name 127 | } 128 | 129 | output "iam_instance_profile_path" { 130 | value = module.session_manager.iam_instance_profile_path 131 | } 132 | 133 | output "iam_instance_profile_role" { 134 | value = module.session_manager.iam_instance_profile_role 135 | } 136 | 137 | output "iam_instance_profile_unique_id" { 138 | value = module.session_manager.iam_instance_profile_unique_id 139 | } 140 | 141 | output "iam_role_arn" { 142 | value = module.session_manager.iam_role_arn 143 | } 144 | 145 | output "iam_role_create_date" { 146 | value = module.session_manager.iam_role_create_date 147 | } 148 | 149 | output "iam_role_unique_id" { 150 | value = module.session_manager.iam_role_unique_id 151 | } 152 | 153 | output "iam_role_name" { 154 | value = module.session_manager.iam_role_name 155 | } 156 | 157 | output "iam_role_description" { 158 | value = module.session_manager.iam_role_description 159 | } 160 | 161 | output "iam_policy_id" { 162 | value = module.session_manager.iam_policy_id 163 | } 164 | 165 | output "iam_policy_arn" { 166 | value = module.session_manager.iam_policy_arn 167 | } 168 | 169 | output "iam_policy_description" { 170 | value = module.session_manager.iam_policy_description 171 | } 172 | 173 | output "iam_policy_name" { 174 | value = module.session_manager.iam_policy_name 175 | } 176 | 177 | output "iam_policy_path" { 178 | value = module.session_manager.iam_policy_path 179 | } 180 | 181 | output "iam_policy_document" { 182 | value = module.session_manager.iam_policy_document 183 | } 184 | -------------------------------------------------------------------------------- /examples/minimal/providers.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "ap-northeast-1" 3 | } 4 | -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | # Terraform module which creates Session Manager resources on AWS. 2 | # 3 | # https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager.html 4 | 5 | # SSM Document 6 | # 7 | # https://docs.aws.amazon.com/systems-manager/latest/userguide/getting-started-configure-preferences-cli.html 8 | 9 | # https://www.terraform.io/docs/providers/aws/r/ssm_document.html 10 | resource "aws_ssm_document" "default" { 11 | name = var.ssm_document_name 12 | document_type = "Session" 13 | document_format = "JSON" 14 | tags = merge({ "Name" = var.ssm_document_name }, var.tags) 15 | 16 | content = jsonencode({ 17 | schemaVersion = "1.0" 18 | description = "Document to hold regional settings for Session Manager" 19 | sessionType = "Standard_Stream" 20 | inputs = { 21 | s3BucketName = var.s3_bucket_name 22 | s3KeyPrefix = var.s3_key_prefix 23 | s3EncryptionEnabled = var.s3_encryption_enabled 24 | cloudWatchLogGroupName = var.cloudwatch_log_group_name 25 | cloudWatchEncryptionEnabled = var.cloudwatch_encryption_enabled 26 | } 27 | }) 28 | } 29 | 30 | # EC2 Instance 31 | # 32 | # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html 33 | 34 | # https://www.terraform.io/docs/providers/aws/r/instance.html 35 | resource "aws_instance" "default" { 36 | ami = local.ami 37 | instance_type = var.instance_type 38 | subnet_id = var.subnet_id 39 | iam_instance_profile = aws_iam_instance_profile.default.name 40 | vpc_security_group_ids = flatten([aws_security_group.default.id, var.vpc_security_group_ids]) 41 | 42 | user_data = var.user_data 43 | tags = merge({ "Name" = var.name }, var.tags) 44 | } 45 | 46 | # https://www.terraform.io/docs/providers/aws/r/security_group.html 47 | resource "aws_security_group" "default" { 48 | name = local.security_group_name 49 | vpc_id = var.vpc_id 50 | description = var.description 51 | tags = merge({ "Name" = local.security_group_name }, var.tags) 52 | } 53 | 54 | # https://www.terraform.io/docs/providers/aws/r/security_group_rule.html 55 | resource "aws_security_group_rule" "egress" { 56 | type = "egress" 57 | from_port = 0 58 | to_port = 0 59 | protocol = "-1" 60 | cidr_blocks = ["0.0.0.0/0"] 61 | security_group_id = aws_security_group.default.id 62 | } 63 | 64 | locals { 65 | ami = var.ami == "" ? data.aws_ami.default.id : var.ami 66 | security_group_name = "${var.name}-session-manager-ec2" 67 | } 68 | 69 | # https://www.terraform.io/docs/providers/aws/d/ami.html#attributes-reference 70 | data "aws_ami" "default" { 71 | most_recent = true 72 | owners = ["amazon"] 73 | 74 | # Describe filters 75 | # https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeImages.html 76 | filter { 77 | name = "owner-alias" 78 | values = ["amazon"] 79 | } 80 | 81 | # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html 82 | filter { 83 | name = "name" 84 | values = ["amzn2-ami-hvm-2.0.????????-x86_64-gp2"] 85 | } 86 | 87 | filter { 88 | name = "state" 89 | values = ["available"] 90 | } 91 | } 92 | 93 | # Session Manager IAM Instance Profile 94 | # 95 | # https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-getting-started-instance-profile.html 96 | 97 | # https://www.terraform.io/docs/providers/aws/r/iam_instance_profile.html 98 | resource "aws_iam_instance_profile" "default" { 99 | name = local.iam_name 100 | role = aws_iam_role.default.name 101 | path = var.iam_path 102 | } 103 | 104 | # https://www.terraform.io/docs/providers/aws/r/iam_role.html 105 | resource "aws_iam_role" "default" { 106 | name = local.iam_name 107 | assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json 108 | path = var.iam_path 109 | description = var.description 110 | tags = merge({ "Name" = local.iam_name }, var.tags) 111 | } 112 | 113 | data "aws_iam_policy_document" "assume_role_policy" { 114 | statement { 115 | actions = ["sts:AssumeRole"] 116 | 117 | principals { 118 | type = "Service" 119 | identifiers = ["ec2.amazonaws.com"] 120 | } 121 | } 122 | } 123 | 124 | # https://www.terraform.io/docs/providers/aws/r/iam_policy.html 125 | resource "aws_iam_policy" "default" { 126 | name = local.iam_name 127 | policy = local.iam_policy 128 | path = var.iam_path 129 | description = var.description 130 | } 131 | 132 | # https://www.terraform.io/docs/providers/aws/r/iam_role_policy_attachment.html 133 | resource "aws_iam_role_policy_attachment" "default" { 134 | role = aws_iam_role.default.name 135 | policy_arn = aws_iam_policy.default.arn 136 | } 137 | 138 | locals { 139 | iam_name = "${var.name}-session-manager" 140 | iam_policy = var.iam_policy == "" ? data.aws_iam_policy.default.policy : var.iam_policy 141 | } 142 | 143 | data "aws_iam_policy" "default" { 144 | arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM" 145 | } 146 | -------------------------------------------------------------------------------- /modules/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmknom/terraform-aws-session-manager/a770f2c0263fc7c87bcd8964dd5fc85ec39531a4/modules/.gitkeep -------------------------------------------------------------------------------- /outputs.tf: -------------------------------------------------------------------------------- 1 | output "ssm_document_description" { 2 | value = aws_ssm_document.default.description 3 | description = "The description of the document." 4 | } 5 | 6 | output "ssm_document_schema_version" { 7 | value = aws_ssm_document.default.schema_version 8 | description = "The schema version of the document." 9 | } 10 | 11 | output "ssm_document_default_version" { 12 | value = aws_ssm_document.default.default_version 13 | description = "The default version of the document." 14 | } 15 | 16 | output "ssm_document_hash" { 17 | value = aws_ssm_document.default.hash 18 | description = "The sha1 or sha256 of the document content." 19 | } 20 | 21 | output "ssm_document_hash_type" { 22 | value = aws_ssm_document.default.hash_type 23 | description = "The hashing algorithm used when hashing the content." 24 | } 25 | 26 | output "ssm_document_latest_version" { 27 | value = aws_ssm_document.default.latest_version 28 | description = "The latest version of the document." 29 | } 30 | 31 | output "ssm_document_owner" { 32 | value = aws_ssm_document.default.owner 33 | description = "The AWS user account of the person who created the document." 34 | } 35 | 36 | output "ssm_document_status" { 37 | value = aws_ssm_document.default.status 38 | description = "The current status of the document." 39 | } 40 | 41 | output "ssm_document_parameter" { 42 | value = aws_ssm_document.default.parameter 43 | description = "The parameters that are available to this document." 44 | } 45 | 46 | output "ssm_document_platform_types" { 47 | value = aws_ssm_document.default.platform_types 48 | description = "A list of OS platforms compatible with this SSM document." 49 | } 50 | 51 | output "instance_id" { 52 | value = aws_instance.default.id 53 | description = "The instance ID." 54 | } 55 | 56 | output "instance_arn" { 57 | value = aws_instance.default.arn 58 | description = "The ARN of the instance." 59 | } 60 | 61 | output "instance_availability_zone" { 62 | value = aws_instance.default.availability_zone 63 | description = "The availability zone of the instance." 64 | } 65 | 66 | output "instance_placement_group" { 67 | value = aws_instance.default.placement_group 68 | description = "The placement group of the instance." 69 | } 70 | 71 | output "instance_key_name" { 72 | value = aws_instance.default.key_name 73 | description = "The key name of the instance." 74 | } 75 | 76 | output "instance_primary_network_interface_id" { 77 | value = aws_instance.default.primary_network_interface_id 78 | description = "The ID of the instance's primary network interface." 79 | } 80 | 81 | output "instance_private_dns" { 82 | value = aws_instance.default.private_dns 83 | description = "The private DNS name assigned to the instance." 84 | } 85 | 86 | output "instance_private_ip" { 87 | value = aws_instance.default.private_ip 88 | description = "The private IP address assigned to the instance." 89 | } 90 | 91 | output "instance_security_groups" { 92 | value = aws_instance.default.security_groups 93 | description = "The associated security groups." 94 | } 95 | 96 | output "instance_subnet_id" { 97 | value = aws_instance.default.subnet_id 98 | description = "The VPC subnet ID." 99 | } 100 | 101 | output "security_group_id" { 102 | value = aws_security_group.default.id 103 | description = "The ID of the security group." 104 | } 105 | 106 | output "security_group_arn" { 107 | value = aws_security_group.default.arn 108 | description = "The ARN of the security group." 109 | } 110 | 111 | output "security_group_vpc_id" { 112 | value = aws_security_group.default.vpc_id 113 | description = "The VPC ID of the security group." 114 | } 115 | 116 | output "security_group_owner_id" { 117 | value = aws_security_group.default.owner_id 118 | description = "The owner ID of the security group." 119 | } 120 | 121 | output "security_group_name" { 122 | value = aws_security_group.default.name 123 | description = "The name of the security group." 124 | } 125 | 126 | output "security_group_description" { 127 | value = aws_security_group.default.description 128 | description = "The description of the security group." 129 | } 130 | 131 | output "security_group_ingress" { 132 | value = aws_security_group.default.ingress 133 | description = "The ingress rules of the security group." 134 | } 135 | 136 | output "security_group_egress" { 137 | value = aws_security_group.default.egress 138 | description = "The egress rules of the security group." 139 | } 140 | 141 | output "iam_instance_profile_id" { 142 | value = aws_iam_instance_profile.default.id 143 | description = "The instance profile's ID." 144 | } 145 | 146 | output "iam_instance_profile_arn" { 147 | value = aws_iam_instance_profile.default.arn 148 | description = "The ARN assigned by AWS to the instance profile." 149 | } 150 | 151 | output "iam_instance_profile_create_date" { 152 | value = aws_iam_instance_profile.default.create_date 153 | description = "The creation timestamp of the instance profile." 154 | } 155 | 156 | output "iam_instance_profile_name" { 157 | value = aws_iam_instance_profile.default.name 158 | description = "The instance profile's name." 159 | } 160 | 161 | output "iam_instance_profile_path" { 162 | value = aws_iam_instance_profile.default.path 163 | description = "The path of the instance profile in IAM." 164 | } 165 | 166 | output "iam_instance_profile_role" { 167 | value = aws_iam_instance_profile.default.role 168 | description = "The role assigned to the instance profile." 169 | } 170 | 171 | output "iam_instance_profile_unique_id" { 172 | value = aws_iam_instance_profile.default.unique_id 173 | description = "The unique ID assigned by AWS." 174 | } 175 | 176 | output "iam_role_arn" { 177 | value = aws_iam_role.default.arn 178 | description = "The Amazon Resource Name (ARN) specifying the IAM Role." 179 | } 180 | 181 | output "iam_role_create_date" { 182 | value = aws_iam_role.default.create_date 183 | description = "The creation date of the IAM Role." 184 | } 185 | 186 | output "iam_role_unique_id" { 187 | value = aws_iam_role.default.unique_id 188 | description = "The stable and unique string identifying the IAM Role." 189 | } 190 | 191 | output "iam_role_name" { 192 | value = aws_iam_role.default.name 193 | description = "The name of the IAM Role." 194 | } 195 | 196 | output "iam_role_description" { 197 | value = aws_iam_role.default.description 198 | description = "The description of the IAM Role." 199 | } 200 | 201 | output "iam_policy_id" { 202 | value = aws_iam_policy.default.id 203 | description = "The IAM Policy's ID." 204 | } 205 | 206 | output "iam_policy_arn" { 207 | value = aws_iam_policy.default.arn 208 | description = "The ARN assigned by AWS to this IAM Policy." 209 | } 210 | 211 | output "iam_policy_description" { 212 | value = aws_iam_policy.default.description 213 | description = "The description of the IAM Policy." 214 | } 215 | 216 | output "iam_policy_name" { 217 | value = aws_iam_policy.default.name 218 | description = "The name of the IAM Policy." 219 | } 220 | 221 | output "iam_policy_path" { 222 | value = aws_iam_policy.default.path 223 | description = "The path of the IAM Policy." 224 | } 225 | 226 | output "iam_policy_document" { 227 | value = aws_iam_policy.default.policy 228 | description = "The policy document of the IAM Policy." 229 | } 230 | -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | variable "name" { 2 | type = string 3 | description = "The name of the Session Manager." 4 | } 5 | 6 | variable "instance_type" { 7 | type = string 8 | description = "The type of instance to start." 9 | } 10 | 11 | variable "subnet_id" { 12 | type = string 13 | description = "The VPC Subnet ID to launch in." 14 | } 15 | 16 | variable "vpc_id" { 17 | type = string 18 | description = "The VPC ID." 19 | } 20 | 21 | variable "ssm_document_name" { 22 | default = "SSM-SessionManagerRunShell" 23 | type = string 24 | description = "The name of the document." 25 | } 26 | 27 | variable "s3_bucket_name" { 28 | default = "" 29 | type = string 30 | description = "The name of the bucket." 31 | } 32 | 33 | variable "s3_key_prefix" { 34 | default = "" 35 | type = string 36 | description = "The prefix for the specified S3 bucket." 37 | } 38 | 39 | variable "s3_encryption_enabled" { 40 | default = true 41 | type = bool 42 | description = "Specify true to indicate that encryption for S3 Bucket enabled." 43 | } 44 | 45 | variable "cloudwatch_log_group_name" { 46 | default = "" 47 | type = string 48 | description = "The name of the log group." 49 | } 50 | 51 | variable "cloudwatch_encryption_enabled" { 52 | default = true 53 | type = bool 54 | description = "Specify true to indicate that encryption for CloudWatch Logs enabled." 55 | } 56 | 57 | variable "ami" { 58 | default = "" 59 | type = string 60 | description = "The AMI to use for the instance." 61 | } 62 | 63 | variable "vpc_security_group_ids" { 64 | default = [] 65 | type = list(string) 66 | description = "A list of security group IDs to associate with." 67 | } 68 | 69 | variable "user_data" { 70 | default = "" 71 | type = string 72 | description = "The user data to provide when launching the instance." 73 | } 74 | 75 | variable "iam_policy" { 76 | default = "" 77 | type = string 78 | description = "The policy document. This is a JSON formatted string." 79 | } 80 | 81 | variable "iam_path" { 82 | default = "/" 83 | type = string 84 | description = "Path in which to create the IAM Role and the IAM Policy." 85 | } 86 | 87 | variable "description" { 88 | default = "Managed by Terraform" 89 | type = string 90 | description = "The description of the all resources." 91 | } 92 | 93 | variable "tags" { 94 | default = {} 95 | type = map(string) 96 | description = "A mapping of tags to assign to all resources." 97 | } 98 | -------------------------------------------------------------------------------- /versions.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_version = ">= 0.12" 3 | } 4 | --------------------------------------------------------------------------------