Skip to content

Commit

Permalink
feat(batch): add default AWS_ACCOUNT and AWS_REGION to Batch containe…
Browse files Browse the repository at this point in the history
…r, if they are not explicitly set (#21041)

Closes #10952

----

### All Submissions:

* [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [X] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [X] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Dzhuneyt authored Jul 21, 2022
1 parent f66f94e commit eed854e
Show file tree
Hide file tree
Showing 31 changed files with 1,823 additions and 22 deletions.
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-batch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ new batch.JobDefinition(this, 'batch-job-def-secrets', {
});
```

It is common practice to invoke other AWS services from within AWS Batch jobs (e.g. using the AWS SDK). For this reason, the AWS_ACCOUNT and AWS_REGION environments are always provided by default to the JobDefinition construct with the values inferred from the current context. You can always overwrite them by setting these environment variables explicitly though.

### Importing an existing Job Definition

#### From ARN
Expand Down
26 changes: 22 additions & 4 deletions packages/@aws-cdk/aws-batch/lib/job-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,11 @@ export class JobDefinition extends Resource implements IJobDefinition {
this.jobDefinitionName = this.getResourceNameAttribute(jobDef.ref);
}

private deserializeEnvVariables(env?: { [name: string]: string }): CfnJobDefinition.EnvironmentProperty[] | undefined {
private deserializeEnvVariables(env?: { [name: string]: string }): CfnJobDefinition.EnvironmentProperty[] {
const vars = new Array<CfnJobDefinition.EnvironmentProperty>();

if (env === undefined) {
return undefined;
return vars;
}

Object.keys(env).map((name: string) => {
Expand Down Expand Up @@ -519,9 +519,27 @@ export class JobDefinition extends Resource implements IJobDefinition {
return undefined;
}

// If the AWS_*** environment variables are not explicitly set to the container, infer them from the current environment.
// This makes the usage of tools like AWS SDK inside the container frictionless

const environment = this.deserializeEnvVariables(container.environment);

if (!environment.find((x) => x.name === 'AWS_REGION')) {
environment.push({
name: 'AWS_REGION',
value: Stack.of(this).region,
});
}
if (!environment.find((x) => x.name === 'AWS_ACCOUNT')) {
environment.push({
name: 'AWS_ACCOUNT',
value: Stack.of(this).account,
});
}

return {
command: container.command,
environment: this.deserializeEnvVariables(container.environment),
environment,
secrets: container.secrets
? Object.entries(container.secrets).map(([key, value]) => {
return {
Expand Down Expand Up @@ -584,4 +602,4 @@ export class JobDefinition extends Resource implements IJobDefinition {
};
});
}
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-batch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"@aws-cdk/assertions": "0.0.0",
"@aws-cdk/cdk-build-tools": "0.0.0",
"@aws-cdk/integ-runner": "0.0.0",
"@aws-cdk/integ-tests": "0.0.0",
"@aws-cdk/cfn2ts": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@types/jest": "^27.5.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "20.0.0",
"files": {
"d3685c79f9ec67f5dd6fda839a136b079f201b3d72695fe0ea3b3788c3471cc8": {
"dd24d4801d80a639d9d7dcd67e4caa9af438a4af95a6243cdebbe188e79ba312": {
"source": {
"path": "batch-stack.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "d3685c79f9ec67f5dd6fda839a136b079f201b3d72695fe0ea3b3788c3471cc8.json",
"objectKey": "dd24d4801d80a639d9d7dcd67e4caa9af438a4af95a6243cdebbe188e79ba312.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,20 @@
"Properties": {
"Type": "container",
"ContainerProperties": {
"Environment": [
{
"Name": "AWS_REGION",
"Value": {
"Ref": "AWS::Region"
}
},
{
"Name": "AWS_ACCOUNT",
"Value": {
"Ref": "AWS::AccountId"
}
}
],
"Image": {
"Fn::Join": [
"",
Expand Down Expand Up @@ -1735,6 +1749,20 @@
"Properties": {
"Type": "container",
"ContainerProperties": {
"Environment": [
{
"Name": "AWS_REGION",
"Value": {
"Ref": "AWS::Region"
}
},
{
"Name": "AWS_ACCOUNT",
"Value": {
"Ref": "AWS::AccountId"
}
}
],
"Image": "docker/whalesay",
"Privileged": false,
"ReadonlyRootFilesystem": false,
Expand Down Expand Up @@ -1806,6 +1834,20 @@
"Properties": {
"Type": "container",
"ContainerProperties": {
"Environment": [
{
"Name": "AWS_REGION",
"Value": {
"Ref": "AWS::Region"
}
},
{
"Name": "AWS_ACCOUNT",
"Value": {
"Ref": "AWS::AccountId"
}
}
],
"ExecutionRoleArn": {
"Fn::GetAtt": [
"executionroleD9A39BE6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,36 @@
"data": "batchspotcomputeenv2CE4DFD9"
}
],
"/batch-stack/batch-demand-compute-env-launch-template-2/Resource-Security-Group/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "batchdemandcomputeenvlaunchtemplate2ResourceSecurityGroupBEA8DDD5"
}
],
"/batch-stack/batch-demand-compute-env-launch-template-2/Ecs-Instance-Role/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "batchdemandcomputeenvlaunchtemplate2EcsInstanceRoleEE146754"
}
],
"/batch-stack/batch-demand-compute-env-launch-template-2/Instance-Profile": [
{
"type": "aws:cdk:logicalId",
"data": "batchdemandcomputeenvlaunchtemplate2InstanceProfileC5A36CBC"
}
],
"/batch-stack/batch-demand-compute-env-launch-template-2/Resource-Service-Instance-Role/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "batchdemandcomputeenvlaunchtemplate2ResourceServiceInstanceRole41CADAC1"
}
],
"/batch-stack/batch-demand-compute-env-launch-template-2/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "batchdemandcomputeenvlaunchtemplate2E12D5CBC"
}
],
"/batch-stack/batch-job-queue/Resource": [
{
"type": "aws:cdk:logicalId",
Expand Down
Loading

0 comments on commit eed854e

Please sign in to comment.