> For the complete documentation index, see [llms.txt](https://annalach.gitbook.io/aws-terraform-workshops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://annalach.gitbook.io/aws-terraform-workshops/sm.md).

# 4. Secrets Manager

{% code title="terraform/secrets/mainf.tf" %}

```bash
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.62.0"
    }
  }

  required_version = ">= 1.0.8"
}

provider "aws" {
  region = "eu-central-1"
}

resource "random_password" "password" {
  length           = 16
  special          = true
  override_special = "_%@"
}

resource "random_id" "db_secret_suffix" {
  byte_length = 8
}

resource "aws_secretsmanager_secret" "db_secret" {
  name = "db-secret-${random_id.db_secret_suffix.b64_std}"
}

resource "aws_secretsmanager_secret_version" "db_secret_version" {
  secret_id = aws_secretsmanager_secret.db_secret.id
  secret_string = jsonencode({
    name     = "workshopsdb"
    username = "workshopsuser"
    password = random_password.password.result
  })
}

```

{% endcode %}

{% hint style="info" %}
If you want to keep database name and username secure you can create input variables and pass its values via environment variables that match the pattern TF\_VAR\_\<VARIABLE\_NAME>. You can learn more about this topic [here](https://learn.hashicorp.com/tutorials/terraform/sensitive-variables?in=terraform/configuration-language). Also, you can use [HashiCorp Vault](https://www.hashicorp.com/products/vault) to manage secrets.
{% endhint %}

{% code title="terraform/secrets/outputs.tf" %}

```bash
output "db_secert_arn" {
  value = aws_secretsmanager_secret_version.db_secret_version.arn
}
```

{% endcode %}

Try to read the secret's value using the AWS CLI:

```bash
$ aws secretsmanager get-secret-value --secret-id aws secretsmanager get-secret-value --secret-id arn:aws:secretsmanager:eu-central-1:852046301552:secret:db-secret-WNqgLUG8tGI=-5EzeJ8

{
    "ARN": "arn:aws:secretsmanager:eu-central-1:852046301552:secret:db-secret-WNqgLUG8tGI=-5EzeJ8",
    "Name": "db-secret-WNqgLUG8tGI=",
    "VersionId": "4FF5D96B-24A9-4712-804B-BCD4D1AF3D6B",
    "SecretString": "{\"name\":\"workshopsdb\",\"password\":\"iypoEyxNfmcWeqnZ\",\"username\":\"workshopsuser\"}",
    "VersionStages": [
        "AWSCURRENT"
    ],
    "CreatedDate": "2021-10-17T21:08:18.435000+02:00"
}
```

Our application will need to read the secret's value to be able to connect to the database. We need to make sure we can read the secret's value from the EC2 instance which is running in the private subnet. First, we need to create IAM Role that the EC2 instance will use.

{% hint style="info" %}
IAM Role is a way of giving some privileges to some AWS resource to let it do something with another AWS resource.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://annalach.gitbook.io/aws-terraform-workshops/sm.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
