Skip to content

Commit 55de190

Browse files
committed
Merge branch 'release/1.4.0'
2 parents c1e8ed6 + 5ef48c2 commit 55de190

21 files changed

+202
-6
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Compiled files
2-
*.tfstate
3-
*.tfstate.backup
2+
*.tfstate*
43

54
# Module directory
65
.terraform/

CHNAGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
88

9+
## [1.4.0] - 2018-08-09
10+
### Added
11+
- Added an option to allow gitlab runner instance to create service linked roles, by default enabled.
12+
- Added example for public subnet
13+
914
## [1.3.0] - 2018-08-08
1015
- Add option to run runners in public subnet
1116

@@ -55,7 +60,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
5560
- Update default AMI's to The latest Amazon Linux AMI 2017.09.1 - released on 2018-01-17.
5661
- Minor updates in the example
5762

58-
[Unreleased]: https://github.com/npalm/terraform-aws-gitlab-runner/compare/1.3.0...HEAD
63+
[Unreleased]: https://github.com/npalm/terraform-aws-gitlab-runner/compare/1.4.0...HEAD
64+
[1.4.0]: https://github.com/npalm/terraform-aws-gitlab-runner/compare/1.3.0...1.4.0
5965
[1.3.0]: https://github.com/npalm/terraform-aws-gitlab-runner/compare/1.2.1...1.3.0
6066
[1.2.1]: https://github.com/npalm/terraform-aws-gitlab-runner/compare/1.2.0...1.2.1
6167
[1.2.0]: https://github.com/npalm/terraform-aws-gitlab-runner/compare/1.1.0...1.2.0

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ export AWS_SECRET_ACCESS_KEY=...
3030
```
3131

3232
### Service linked roles
33-
Currently the ec2 instance role does not allow creation of service linked roles. The runner instances is depended on the following two service linked roles:
33+
The gitlab runner ec2 instance needs the following sercice linked roles:
3434
- AWSServiceRoleForAutoScaling
3535
- AWSServiceRoleForEC2Spot
3636

37-
You can create them manually or via terraform.
37+
By default the ec2 instance is allowed to create the roles, by setting the option `allow_iam_service_linked_role_creation` to `false` you can deny the creation of roles by the instance. In that case you have to ensure the roles exists. You can create them manually or via terraform.
3838

3939
```
4040
resource "aws_iam_service_linked_role" "spot" {

examples/runner-default/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Example - Runner - Private subnets
2+
3+
Example how create a gitlab runner, running in a private subnet.
4+
5+
## Prerequisite
6+
The terraform version is managed using [tfenv](https://github.com/Zordrak/tfenv). If you are not using tfenv please check `.terraform-version` for the tested version.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.11.7

examples/runner-public/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Example - Runner - Public subnets
2+
3+
Example how create a gitlab runner, running in a public subnet.
4+
5+
## Prerequisite
6+
The terraform version is managed using [tfenv](https://github.com/Zordrak/tfenv). If you are not using tfenv please check `.terraform-version` for the tested version.

examples/runner-public/key.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
resource "tls_private_key" "ssh" {
2+
algorithm = "RSA"
3+
}
4+
5+
resource "local_file" "public_ssh_key" {
6+
depends_on = ["tls_private_key.ssh"]
7+
8+
content = "${tls_private_key.ssh.public_key_openssh}"
9+
filename = "${var.public_ssh_key_filename}"
10+
}
11+
12+
resource "local_file" "private_ssh_key" {
13+
depends_on = ["tls_private_key.ssh"]
14+
15+
content = "${tls_private_key.ssh.private_key_pem}"
16+
filename = "${var.private_ssh_key_filename}"
17+
}
18+
19+
resource "null_resource" "file_permission" {
20+
depends_on = ["local_file.private_ssh_key"]
21+
22+
provisioner "local-exec" {
23+
command = "${format("chmod 600 %s", var.private_ssh_key_filename)}"
24+
}
25+
}

examples/runner-public/main.tf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module "vpc" {
2+
source = "terraform-aws-modules/vpc/aws"
3+
version = "1.37.0"
4+
5+
name = "vpc-${var.environment}"
6+
cidr = "10.1.0.0/16"
7+
8+
azs = ["eu-west-1a"]
9+
public_subnets = ["10.1.101.0/24"]
10+
11+
tags = {
12+
Environment = "${var.environment}"
13+
}
14+
}
15+
16+
module "runner" {
17+
source = "../../"
18+
19+
aws_region = "${var.aws_region}"
20+
environment = "${var.environment}"
21+
22+
ssh_public_key = "${local_file.public_ssh_key.content}"
23+
24+
runners_use_private_address = false
25+
26+
vpc_id = "${module.vpc.vpc_id}"
27+
subnet_id_gitlab_runner = "${element(module.vpc.public_subnets, 0)}"
28+
subnet_id_runners = "${element(module.vpc.public_subnets, 0)}"
29+
30+
runners_name = "${var.runner_name}"
31+
runners_gitlab_url = "${var.gitlab_url}"
32+
runners_token = "${var.runner_token}"
33+
34+
runners_off_peak_timezone = "Europe/Amsterdam"
35+
runners_off_peak_idle_count = 0
36+
runners_off_peak_idle_time = 60
37+
38+
# working 9 to 5 :)
39+
runners_off_peak_periods = "[\"* * 0-9,17-23 * * mon-fri *\", \"* * * * * sat,sun *\"]"
40+
}

examples/runner-public/providers.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
provider "aws" {
2+
region = "${var.aws_region}"
3+
version = "1.23"
4+
}
5+
6+
provider "template" {
7+
version = "1.0"
8+
}
9+
10+
provider "local" {
11+
version = "1.1"
12+
}
13+
14+
provider "null" {
15+
version = "1.0"
16+
}
17+
18+
provider "tls" {
19+
version = "1.1"
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
key_name = "gitlab-runner"
2+
3+
environment = "runner-public"
4+
5+
aws_region = "eu-west-1"
6+
7+
# Add the following variables:
8+
runner_name = "docker.m3"
9+
10+
gitlab_url = "https://gitlab.com"
11+
12+
runner_token = "3939146918cced54ecf1dd08e6b87e"

examples/runner-public/variables.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
variable "aws_region" {
2+
description = "AWS region."
3+
type = "string"
4+
default = "eu-west-1"
5+
}
6+
7+
variable "environment" {
8+
description = "A name that indentifies the environment, will used as prefix and for taggin."
9+
default = "ci-runners"
10+
type = "string"
11+
}
12+
13+
variable "public_ssh_key_filename" {
14+
default = "generated/id_rsa.pub"
15+
}
16+
17+
variable "private_ssh_key_filename" {
18+
default = "generated/id_rsa"
19+
}
20+
21+
variable "runner_name" {
22+
description = "Name of the runner, will be used in the runner config.toml"
23+
type = "string"
24+
}
25+
26+
variable "gitlab_url" {
27+
description = "URL of the gitlab instance to connect to."
28+
type = "string"
29+
}
30+
31+
variable "runner_token" {
32+
description = "Token for the runner, will be used in the runner config.toml"
33+
type = "string"
34+
}

main.tf

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ resource "aws_launch_configuration" "gitlab_runner_instance" {
151151
}
152152
}
153153

154+
################################################################################
155+
### Trust policy
156+
################################################################################
154157
resource "aws_iam_instance_profile" "instance" {
155158
name = "${var.environment}-instance-profile"
156159
role = "${aws_iam_role.instance.name}"
@@ -165,6 +168,9 @@ resource "aws_iam_role" "instance" {
165168
assume_role_policy = "${data.template_file.instance_role_trust_policy.rendered}"
166169
}
167170

171+
################################################################################
172+
### docker machine instance policy
173+
################################################################################
168174
data "template_file" "docker_machine_policy" {
169175
template = "${file("${path.module}/policies/instance-docker-machine-policy.json")}"
170176
}
@@ -177,7 +183,33 @@ resource "aws_iam_policy" "docker_machine" {
177183
policy = "${data.template_file.docker_machine_policy.rendered}"
178184
}
179185

180-
resource "aws_iam_role_policy_attachment" "test-attach" {
186+
resource "aws_iam_role_policy_attachment" "docker_machine" {
181187
role = "${aws_iam_role.instance.name}"
182188
policy_arn = "${aws_iam_policy.docker_machine.arn}"
183189
}
190+
191+
################################################################################
192+
### Service linked policy, optional
193+
################################################################################
194+
data "template_file" "service_linked_role" {
195+
count = "${var.allow_iam_service_linked_role_creation ? 1 : 0}"
196+
197+
template = "${file("${path.module}/policies/service-linked-role-create-policy.json")}"
198+
}
199+
200+
resource "aws_iam_policy" "service_linked_role" {
201+
count = "${var.allow_iam_service_linked_role_creation ? 1 : 0}"
202+
203+
name = "${var.environment}-service_linked_role"
204+
path = "/"
205+
description = "Policy for creation of service linked roles."
206+
207+
policy = "${data.template_file.service_linked_role.rendered}"
208+
}
209+
210+
resource "aws_iam_role_policy_attachment" "service_linked_role" {
211+
count = "${var.allow_iam_service_linked_role_creation ? 1 : 0}"
212+
213+
role = "${aws_iam_role.instance.name}"
214+
policy_arn = "${aws_iam_policy.service_linked_role.arn}"
215+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Action": "iam:CreateServiceLinkedRole",
7+
"Resource": "arn:aws:iam::*:role/aws-service-role/*"
8+
}
9+
]
10+
}

variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,8 @@ variable "tags" {
182182
description = "Map of tags that will be added to created resources. By default resources will be taggen with name and environemnt."
183183
default = {}
184184
}
185+
186+
variable "allow_iam_service_linked_role_creation" {
187+
description = "Attach policy to runner instance to create service linked roles."
188+
default = true
189+
}

0 commit comments

Comments
 (0)