# Secret

A secret environment variable.

Obtainable from: Secret.fromSecretsManager, Secret.fromSecretsManagerVersion, Secret.fromSsmParameter

- Source: [`aws-batch/lib/ecs-container-definition.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-batch/lib/ecs-container-definition.ts#L43)
- Type last changed: v2.96.0 · 13 Sep 2023

## Example

TypeScript:

```ts
declare const mySecret: secretsmanager.ISecret;

const jobDefn = new batch.EcsJobDefinition(this, 'JobDefn', {
  container: new batch.EcsEc2ContainerDefinition(this, 'containerDefn', {
    image: ecs.ContainerImage.fromRegistry('public.ecr.aws/amazonlinux/amazonlinux:latest'),
    memory: cdk.Size.mebibytes(2048),
    cpu: 256,
    secrets: {
      MY_SECRET_ENV_VAR: batch.Secret.fromSecretsManager(mySecret),
    }
  }),
});
```

Python:

```python
# my_secret: secretsmanager.ISecret

job_defn = batch.EcsJobDefinition(self, "JobDefn",
    container=batch.EcsEc2ContainerDefinition(self, "containerDefn",
        image=ecs.ContainerImage.from_registry("public.ecr.aws/amazonlinux/amazonlinux:latest"),
        memory=cdk.Size.mebibytes(2048),
        cpu=256,
        secrets={
            "MY_SECRET_ENV_VAR": batch.Secret.from_secrets_manager(my_secret)
        }
    )
)
```

Java:

```java
ISecret mySecret;

EcsJobDefinition jobDefn = EcsJobDefinition.Builder.create(this, "JobDefn")
        .container(EcsEc2ContainerDefinition.Builder.create(this, "containerDefn")
                .image(ContainerImage.fromRegistry("public.ecr.aws/amazonlinux/amazonlinux:latest"))
                .memory(Size.mebibytes(2048))
                .cpu(256)
                .secrets(Map.of(
                        "MY_SECRET_ENV_VAR", Secret.fromSecretsManager(mySecret)))
                .build())
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
ISecret mySecret;

var jobDefn = new EcsJobDefinition(this, "JobDefn", new EcsJobDefinitionProps {
    Container = new EcsEc2ContainerDefinition(this, "containerDefn", new EcsEc2ContainerDefinitionProps {
        Image = ContainerImage.FromRegistry("public.ecr.aws/amazonlinux/amazonlinux:latest"),
        Memory = Size.Mebibytes(2048),
        Cpu = 256,
        Secrets = new Dictionary<string, Secret> {
            { "MY_SECRET_ENV_VAR", Secret.FromSecretsManager(mySecret) }
        }
    })
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var mySecret ISecret

jobDefn := batch.NewEcsJobDefinition(this, jsii.String("JobDefn"), &EcsJobDefinitionProps{
	Container: batch.NewEcsEc2ContainerDefinition(this, jsii.String("containerDefn"), &EcsEc2ContainerDefinitionProps{
		Image: ecs.ContainerImage_FromRegistry(jsii.String("public.ecr.aws/amazonlinux/amazonlinux:latest")),
		Memory: cdk.Size_Mebibytes(jsii.Number(2048)),
		Cpu: jsii.Number(256),
		Secrets: map[string]Secret{
			"MY_SECRET_ENV_VAR": batch.Secret_fromSecretsManager(mySecret),
		},
	}),
})
```

## Initializer

TypeScript:

```ts
new Secret()
```

Python:

```python
Secret()
```

Java:

```java
new Secret()
```

C# / .NET:

```csharp
new Secret()
```

Golang:

```go
NewSecret()
```

## Properties

- `arn`: string — The ARN of the secret.

- `hasField`: boolean — Whether this secret uses a specific JSON field.

## Methods

- `fromSecretsManager(secret: ISecret, field?: string): Secret // chainable` (static) — Creates a environment variable value from a secret stored in AWS Secrets Manager.

  **Parameters**

  - `secret` — the secret stored in AWS Secrets Manager.
  - `field` — the name of the field with the value that you want to set as the environment variable value.

- `fromSecretsManagerVersion(secret: ISecret, versionInfo: SecretVersionInfo, field?: string): Secret // chainable` (static) — Creates a environment variable value from a secret stored in AWS Secrets Manager.

  **Parameters**

  - `secret` — the secret stored in AWS Secrets Manager.
  - `versionInfo` — the version information to reference the secret.
  - `field` — the name of the field with the value that you want to set as the environment variable value.

- `fromSsmParameter(parameter: IParameter): Secret // chainable` (static) — Creates an environment variable value from a parameter stored in AWS Systems Manager Parameter Store.

- `grantRead(grantee: IGrantable): Grant` — Grants reading the secret to a principal [disable-awslint:no-grants].
