Skip to content

Commit

Permalink
Merge branch 'release/1.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
npalm committed Aug 8, 2018
2 parents c1e8ed6 + 5ef48c2 commit 55de190
Show file tree
Hide file tree
Showing 21 changed files with 202 additions and 6 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Compiled files
*.tfstate
*.tfstate.backup
*.tfstate*

# Module directory
.terraform/
Expand Down
8 changes: 7 additions & 1 deletion CHNAGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [1.4.0] - 2018-08-09
### Added
- Added an option to allow gitlab runner instance to create service linked roles, by default enabled.
- Added example for public subnet

## [1.3.0] - 2018-08-08
- Add option to run runners in public subnet

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

[Unreleased]: https://github.com/npalm/terraform-aws-gitlab-runner/compare/1.3.0...HEAD
[Unreleased]: https://github.com/npalm/terraform-aws-gitlab-runner/compare/1.4.0...HEAD
[1.4.0]: https://github.com/npalm/terraform-aws-gitlab-runner/compare/1.3.0...1.4.0
[1.3.0]: https://github.com/npalm/terraform-aws-gitlab-runner/compare/1.2.1...1.3.0
[1.2.1]: https://github.com/npalm/terraform-aws-gitlab-runner/compare/1.2.0...1.2.1
[1.2.0]: https://github.com/npalm/terraform-aws-gitlab-runner/compare/1.1.0...1.2.0
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ export AWS_SECRET_ACCESS_KEY=...
```

### Service linked roles
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:
The gitlab runner ec2 instance needs the following sercice linked roles:
- AWSServiceRoleForAutoScaling
- AWSServiceRoleForEC2Spot

You can create them manually or via terraform.
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.

```
resource "aws_iam_service_linked_role" "spot" {
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions examples/runner-default/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Example - Runner - Private subnets

Example how create a gitlab runner, running in a private subnet.

## Prerequisite
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.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions examples/runner-public/.terraform-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.11.7
6 changes: 6 additions & 0 deletions examples/runner-public/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Example - Runner - Public subnets

Example how create a gitlab runner, running in a public subnet.

## Prerequisite
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.
25 changes: 25 additions & 0 deletions examples/runner-public/key.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
resource "tls_private_key" "ssh" {
algorithm = "RSA"
}

resource "local_file" "public_ssh_key" {
depends_on = ["tls_private_key.ssh"]

content = "${tls_private_key.ssh.public_key_openssh}"
filename = "${var.public_ssh_key_filename}"
}

resource "local_file" "private_ssh_key" {
depends_on = ["tls_private_key.ssh"]

content = "${tls_private_key.ssh.private_key_pem}"
filename = "${var.private_ssh_key_filename}"
}

resource "null_resource" "file_permission" {
depends_on = ["local_file.private_ssh_key"]

provisioner "local-exec" {
command = "${format("chmod 600 %s", var.private_ssh_key_filename)}"
}
}
40 changes: 40 additions & 0 deletions examples/runner-public/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "1.37.0"

name = "vpc-${var.environment}"
cidr = "10.1.0.0/16"

azs = ["eu-west-1a"]
public_subnets = ["10.1.101.0/24"]

tags = {
Environment = "${var.environment}"
}
}

module "runner" {
source = "../../"

aws_region = "${var.aws_region}"
environment = "${var.environment}"

ssh_public_key = "${local_file.public_ssh_key.content}"

runners_use_private_address = false

vpc_id = "${module.vpc.vpc_id}"
subnet_id_gitlab_runner = "${element(module.vpc.public_subnets, 0)}"
subnet_id_runners = "${element(module.vpc.public_subnets, 0)}"

runners_name = "${var.runner_name}"
runners_gitlab_url = "${var.gitlab_url}"
runners_token = "${var.runner_token}"

runners_off_peak_timezone = "Europe/Amsterdam"
runners_off_peak_idle_count = 0
runners_off_peak_idle_time = 60

# working 9 to 5 :)
runners_off_peak_periods = "[\"* * 0-9,17-23 * * mon-fri *\", \"* * * * * sat,sun *\"]"
}
20 changes: 20 additions & 0 deletions examples/runner-public/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
provider "aws" {
region = "${var.aws_region}"
version = "1.23"
}

provider "template" {
version = "1.0"
}

provider "local" {
version = "1.1"
}

provider "null" {
version = "1.0"
}

provider "tls" {
version = "1.1"
}
12 changes: 12 additions & 0 deletions examples/runner-public/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
key_name = "gitlab-runner"

environment = "runner-public"

aws_region = "eu-west-1"

# Add the following variables:
runner_name = "docker.m3"

gitlab_url = "https://gitlab.com"

runner_token = "3939146918cced54ecf1dd08e6b87e"
34 changes: 34 additions & 0 deletions examples/runner-public/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
variable "aws_region" {
description = "AWS region."
type = "string"
default = "eu-west-1"
}

variable "environment" {
description = "A name that indentifies the environment, will used as prefix and for taggin."
default = "ci-runners"
type = "string"
}

variable "public_ssh_key_filename" {
default = "generated/id_rsa.pub"
}

variable "private_ssh_key_filename" {
default = "generated/id_rsa"
}

variable "runner_name" {
description = "Name of the runner, will be used in the runner config.toml"
type = "string"
}

variable "gitlab_url" {
description = "URL of the gitlab instance to connect to."
type = "string"
}

variable "runner_token" {
description = "Token for the runner, will be used in the runner config.toml"
type = "string"
}
34 changes: 33 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ resource "aws_launch_configuration" "gitlab_runner_instance" {
}
}

################################################################################
### Trust policy
################################################################################
resource "aws_iam_instance_profile" "instance" {
name = "${var.environment}-instance-profile"
role = "${aws_iam_role.instance.name}"
Expand All @@ -165,6 +168,9 @@ resource "aws_iam_role" "instance" {
assume_role_policy = "${data.template_file.instance_role_trust_policy.rendered}"
}

################################################################################
### docker machine instance policy
################################################################################
data "template_file" "docker_machine_policy" {
template = "${file("${path.module}/policies/instance-docker-machine-policy.json")}"
}
Expand All @@ -177,7 +183,33 @@ resource "aws_iam_policy" "docker_machine" {
policy = "${data.template_file.docker_machine_policy.rendered}"
}

resource "aws_iam_role_policy_attachment" "test-attach" {
resource "aws_iam_role_policy_attachment" "docker_machine" {
role = "${aws_iam_role.instance.name}"
policy_arn = "${aws_iam_policy.docker_machine.arn}"
}

################################################################################
### Service linked policy, optional
################################################################################
data "template_file" "service_linked_role" {
count = "${var.allow_iam_service_linked_role_creation ? 1 : 0}"

template = "${file("${path.module}/policies/service-linked-role-create-policy.json")}"
}

resource "aws_iam_policy" "service_linked_role" {
count = "${var.allow_iam_service_linked_role_creation ? 1 : 0}"

name = "${var.environment}-service_linked_role"
path = "/"
description = "Policy for creation of service linked roles."

policy = "${data.template_file.service_linked_role.rendered}"
}

resource "aws_iam_role_policy_attachment" "service_linked_role" {
count = "${var.allow_iam_service_linked_role_creation ? 1 : 0}"

role = "${aws_iam_role.instance.name}"
policy_arn = "${aws_iam_policy.service_linked_role.arn}"
}
10 changes: 10 additions & 0 deletions policies/service-linked-role-create-policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iam:CreateServiceLinkedRole",
"Resource": "arn:aws:iam::*:role/aws-service-role/*"
}
]
}
5 changes: 5 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,8 @@ variable "tags" {
description = "Map of tags that will be added to created resources. By default resources will be taggen with name and environemnt."
default = {}
}

variable "allow_iam_service_linked_role_creation" {
description = "Attach policy to runner instance to create service linked roles."
default = true
}

0 comments on commit 55de190

Please sign in to comment.