Skip to content

Commit

Permalink
Refactor module to better support k8s requirement with fluentd (#3)
Browse files Browse the repository at this point in the history
* Refactor module to better support k8s requirement with fluentd (#3)
  • Loading branch information
goruha authored Mar 29, 2018
1 parent 2243829 commit 9e796e2
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 141 deletions.
36 changes: 36 additions & 0 deletions .README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## {{ (datasource "git").name }} [![Build Status](https://travis-ci.org/cloudposse/terraform-aws-cloudwatch-logs.svg?branch=master)](https://travis-ci.org/cloudposse/terraform-aws-cloudwatch-logs)
{{ (datasource "section").warning }}

Terraform module for creation of CloudWatch Log Streams and Log Groups for use with Fluentd.

## Usage

```terraform
module "cloudwatch_log" {
namespace = "${var.namespace}"
stage = "${var.stage}"
stream_names = ["kafka-instance-1", "kafka-instance-2"]
}
```

{{ (datasource "terraform").input }}

{{ (datasource "terraform").output }}

{{ (datasource "section").help }}

{{ (datasource "section").contributing }}

{{ (datasource "license").apache2 }}

{{ (datasource "section").about }}

### Contributors

|
{{- (datasource "contributor").erik }} |
{{- (datasource "contributor").vladimir }} |
{{- (datasource "contributor").igor }} |
{{ (datasource "contributor")._3 }}

{{ (datasource "contributor")._links }}
150 changes: 119 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# terraform-aws-cloudwatch-logs [![Build Status](https://travis-ci.org/cloudposse/terraform-aws-cloudwatch-logs.svg?branch=master)](https://travis-ci.org/cloudposse/terraform-aws-cloudwatch-logs)
## terraform-aws-cloudwatch-logs [![Build Status](https://travis-ci.org/cloudposse/terraform-aws-cloudwatch-logs.svg?branch=master)](https://travis-ci.org/cloudposse/terraform-aws-cloudwatch-logs)
<!---
--- This file was automatically generated by the `build-harness`
--- Make changes instead to `.README.md` and rebuild.
--->

Terraform module for creation streams and group for them.
Terraform module for creation of CloudWatch Log Streams and Log Groups for use with Fluentd.

## Usage

Expand All @@ -12,35 +16,119 @@ module "cloudwatch_log" {
}
```

## Inputs

| Name | Default | Description | Required |
|:--------------------|:-------------:|:----------------------------------------------------------------|:--------:|
| `namespace` | `` | Namespace (e.g. `cp` or `cloudposse`) | Yes |
| `stage` | `` | Stage (e.g. `prod`, `dev`, `staging`) | Yes |
| `name` | `` | Name (e.g. `bastion` or `db`) | No |
| `delimiter` | `-` | Delimiter to be used between `name`, `namespace`, `stage`, etc. | No |
| `attributes` | `[]` | Additional attributes (e.g. `policy` or `role`) | No |
| `tags` | `{}` | Additional tags (e.g. `map("BusinessUnit","XYZ")` | No |
| `retention_in_days` | `30` | Number of days you want to retain log events in the log group | No |
| `stream_names` | `["default"]` | List names of streams | No |
| `user_enabled` | `true` | Flag for creation user | No |
| `region` | `` | AWS region, by default used the region of caller | No |

## Outputs

| Name | Description |
|:--------------------|:----------------------|
| `log_group_arn` | ARN of the log group |
| `access_key_id` | Access key ID |
| `secret_access_key` | Secret access key |
| `stream_arns` | ARN of the log stream |
| `user_arn` | ARN of AWS user |
| `user_name` | AWS username |
| `user_unique_id` | ID of user |
| `log_stream_names` | Name of log streams |
| `log_group_name` | Name of log group |
## Input

<!--------------------------------REQUIRE POSTPROCESSING-------------------------------->
| Name | Default | Description |
|:------|:---------:|:--------------:|
| additional_permissions |[] |Additional permissions granted to assumed role|
| attributes |[] |Additional attributes (e.g. `policy` or `role`)|
| delimiter |"-" |Delimiter to be used between `name`, `namespace`, `stage`, etc.|
| name |"" |Name (e.g. `bastion` or `db`)|
| namespace |__REQUIRED__ |Namespace (e.g. `cp` or `cloudposse`)|
| retention_in_days |"30" |Number of days you want to retain log events in the log group|
| stage |__REQUIRED__ |Stage (e.g. `prod`, `dev`, `staging`)|
| stream_names |[] |Names of streams|
| tags |{} |Additional tags (e.g. map(`BusinessUnit`,`XYZ`)|
| trusted_roles |[] |Roles allow to assume role|

## Output

<!--------------------------------REQUIRE POSTPROCESSING-------------------------------->
| Name | Description |
|:------|:------------:|
| assumed_role_arn | ARN of role to assume |
| assumed_role_name | Name of role to assume |
| log_group_arn | ARN of the log group |
| log_group_name | Name of log group |
| policy_arn | ARN of role to assume |
| policy_name | Name of role to assume |
| stream_arns | ARN of the log stream |

## Help

**Got a question?**

File a GitHub [issue](https://github.com/cloudposse/terraform-aws-cloudwatch-logs/issues), send us an [email](mailto:hello@cloudposse.com) or reach out to us on [Gitter](https://gitter.im/cloudposse/).

## Contributing

### Bug Reports & Feature Requests

Please use the [issue tracker](https://github.com/cloudposse/terraform-aws-cloudwatch-logs/issues) to report any bugs or file feature requests.

### Developing

If you are interested in being a contributor and want to get involved in developing `terraform-aws-cloudwatch-logs`, we would love to hear from you! Shoot us an [email](mailto:hello@cloudposse.com).

In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow.

1. **Fork** the repo on GitHub
2. **Clone** the project to your own machine
3. **Commit** changes to your own branch
4. **Push** your work back up to your fork
5. Submit a **Pull request** so that we can review your changes

**NOTE:** Be sure to merge the latest from "upstream" before making a pull request!

## License

Apache 2 License. See [`LICENSE`](LICENSE) for full details.
[APACHE 2.0](LICENSE) © 2017 [Cloud Posse, LLC](https://cloudposse.com)

See [LICENSE](LICENSE) for full details.

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.

## About

This project is maintained and funded by [Cloud Posse, LLC][website]. Like it? Please let us know at <hello@cloudposse.com>

We love [Open Source Software](https://github.com/cloudposse/)!

See [our other projects][community]
or [hire us][hire] to help build your next cloud-platform.

[website]: http://cloudposse.com/
[community]: https://github.com/cloudposse/
[hire]: http://cloudposse.com/contact/

### Contributors

|[![Erik Osterman][erik_img]][erik_web]<br/>[Erik Osterman][erik_web] |[![Vladimir][vladimir_img]][vladimir_web]<br/>[Vladimir][vladimir_web] |[![Igor Rodionov][igor_img]][igor_web]<br/>[Igor Rodionov][igor_img] |
|---|---|---|

[andriy_img]: https://avatars0.githubusercontent.com/u/7356997?v=4&u=ed9ce1c9151d552d985bdf5546772e14ef7ab617&s=144
[andriy_web]: https://github.com/aknysh/

[erik_img]: http://s.gravatar.com/avatar/88c480d4f73b813904e00a5695a454cb?s=144
[erik_web]: https://github.com/osterman/

[igor_img]: http://s.gravatar.com/avatar/bc70834d32ed4517568a1feb0b9be7e2?s=144
[igor_web]: https://github.com/goruha/

[konstantin_img]: https://avatars1.githubusercontent.com/u/11299538?v=4&u=ed9ce1c9151d552d985bdf5546772e14ef7ab617&s=144
[konstantin_web]: https://github.com/comeanother/

[sergey_img]: https://avatars1.githubusercontent.com/u/1134449?v=4&u=ed9ce1c9151d552d985bdf5546772e14ef7ab617&s=144
[sergey_web]: https://github.com/s2504s/

[valeriy_img]: https://avatars1.githubusercontent.com/u/10601658?v=4&u=ed9ce1c9151d552d985bdf5546772e14ef7ab617&s=144
[valeriy_web]: https://github.com/drama17/

[vladimir_img]: https://avatars1.githubusercontent.com/u/26582191?v=4&u=ed9ce1c9151d552d985bdf5546772e14ef7ab617&s=144
[vladimir_web]: https://github.com/SweetOps/
61 changes: 44 additions & 17 deletions iam.tf
Original file line number Diff line number Diff line change
@@ -1,39 +1,66 @@
data "aws_iam_policy_document" "log_assume" {
data "aws_iam_policy_document" "role_trust" {
statement {
actions = ["sts:AssumeRole"]

principals {
type = "Service"
identifiers = ["logs.${length(var.region) > 0 ? var.region: data.aws_region.default.name}.amazonaws.com"]
identifiers = ["ec2.amazonaws.com"]
}
}

statement {
actions = ["sts:AssumeRole"]

principals {
type = "AWS"

identifiers = ["${var.trusted_roles}"]
}
}
}

resource "aws_iam_role" "default" {
name = "${module.label.id}"
assume_role_policy = "${data.aws_iam_policy_document.role_trust.json}"
}

data "aws_iam_policy_document" "log" {
resource "aws_iam_policy" "default" {
name = "${module.label.id}"
policy = "${data.aws_iam_policy_document.log_agent.json}"
}

data "aws_iam_policy_document" "log_agent" {
statement {
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams",
"logs:CreateLogStream",
"logs:DeleteLogStream",
]

resources = ["*"]
}

statement {
actions = [
"logs:PutLogEvents",
]

resources = [
"${join(",", compact(concat(list(aws_cloudwatch_log_group.default.arn), aws_cloudwatch_log_stream.default.*.arn)))}",
"${aws_cloudwatch_log_group.default.arn}",
]
}
}

resource "aws_iam_role" "log" {
name = "${module.log_group_label.id}"
assume_role_policy = "${data.aws_iam_policy_document.log_assume.json}"
statement {
actions = [
"${var.additional_permissions}",
]

resources = [
"${aws_cloudwatch_log_group.default.arn}",
]
}
}

resource "aws_iam_role_policy" "log" {
name = "${module.log_group_label.id}"
role = "${aws_iam_role.log.id}"
policy = "${data.aws_iam_policy_document.log.json}"
resource "aws_iam_role_policy_attachment" "default" {
role = "${aws_iam_role.default.name}"
policy_arn = "${aws_iam_policy.default.arn}"
}
33 changes: 0 additions & 33 deletions iam_user.tf

This file was deleted.

24 changes: 5 additions & 19 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
data "aws_region" "default" {
current = "true"
}

module "log_group_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.1"
module "label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.3"
namespace = "${var.namespace}"
name = "${var.name}"
stage = "${var.stage}"
Expand All @@ -12,24 +8,14 @@ module "log_group_label" {
tags = "${var.tags}"
}

module "stream_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.1"
namespace = "${var.namespace}"
name = "${var.name}"
stage = "${var.stage}"
delimiter = "${var.delimiter}"
attributes = "${compact(concat(var.attributes, list("stream")))}"
tags = "${var.tags}"
}

resource "aws_cloudwatch_log_group" "default" {
name = "${module.log_group_label.id}"
name = "${module.label.id}"
retention_in_days = "${var.retention_in_days}"
tags = "${module.log_group_label.tags}"
tags = "${module.label.tags}"
}

resource "aws_cloudwatch_log_stream" "default" {
count = "${length(var.stream_names)}"
name = "${module.stream_label.id}${var.delimiter}${element(var.stream_names, count.index)}"
name = "${element(var.stream_names, count.index)}"
log_group_name = "${aws_cloudwatch_log_group.default.name}"
}
Loading

0 comments on commit 9e796e2

Please sign in to comment.