# terraform-service-info
Terraform module for fetching service security group ids from the resource explorer for cross-account access. application_family: devops

## Usage
Example of usage with a single service:
```
module "service_info" {
  source = ""git@github.com:theorchard/terraform-service-info.git//?ref=1.0.0"
  service_name = "ows-example"
  environment  = "qa"
}

# Requires terraform-fargate module version >= 6.3.0
module "fargate" {
  ...
  https_listener_allow_security_group_ids = module.service_info.security_groups
  ...
}

module "rds" {
  ...
  rds_ingress_security_groups = module.service_info.security_groups
  ...
}

module "elasticache" {
  ...
  additional_source_security_group_ids = module.service_info.security_groups
  ...
}

# Requires terraform-elasticsearch module version >= 5.1.0
module "elasticsearch" {
  ...
  https_allowed_custom_security_groups = module.service_info.security_groups
  ...
}
```

Fetching security groups for multiple services:
```
locals {
  services = ["ows-example-1", "ows-example-2"]
}

module "services_info" {
  source = ""git@github.com:theorchard/terraform-service-info.git//?ref=1.0.0"
  for_each = toset(local.services)
  service_name = each.value
  environment  = "qa"
}

# Requires fargate module version >= 6.5.0
module "fargate" {
  ...
  https_listener_allow_security_group_ids = flatten([for service in module.services_info: service.security_groups])
  ...
}

module "rds" {
  ...
  rds_ingress_security_groups = flatten([for service in module.services_info: service.security_groups])
  ...
}

module "elasticache" {
  ...
  additional_source_security_group_ids = flatten([for service in module.services_info: service.security_groups])
  ...
}

# Requires terraform-elasticsearch module version >= 5.1.0
module "elasticsearch" {
  ...
  https_allowed_custom_security_groups = flatten([for service in module.services_info: service.security_groups])
  ...
}
```

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 6.0.0 |

## Resources

| Name | Type |
|------|------|
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_resourceexplorer2_search.sg_search](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/resourceexplorer2_search) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_environment"></a> [environment](#input\_environment) | n/a | `string` | n/a | yes |
| <a name="input_security_group_type"></a> [security\_group\_type](#input\_security\_group\_type) | Type of the security group to search for. Valid values are: lb, task, lambda. | `string` | `"task"` | no |
| <a name="input_service_name"></a> [service\_name](#input\_service\_name) | n/a | `string` | n/a | yes |
| <a name="input_view_arn"></a> [view\_arn](#input\_view\_arn) | ARN of Resource explorer view to search resources in. | `string` | `"arn:aws:resource-explorer-2:us-east-1:970903126758:view/all-pde-security-groups/a0508e0f-f13e-42ac-8595-d2793416ff80"` | no |
| <a name="input_view_region"></a> [view\_region](#input\_view\_region) | n/a | `string` | `"us-east-1"` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_security_groups"></a> [security\_groups](#output\_security\_groups) | List of security groups in filtered by current region, formatted with account\_id prefix. |

## Changelog

### 1.0.0
- Initial release

### 1.1.0
- Removed the current account security groups from the results to avoid terraform drift when the service in the same account.
- Updated an error message when no security groups are found.
