Skip to content

Commit 669d3dd

Browse files
authored
Merge branch 'main' into read-consistency
2 parents 1e24775 + eed854e commit 669d3dd

31 files changed

Lines changed: 1823 additions & 22 deletions

packages/@aws-cdk/aws-batch/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ new batch.JobDefinition(this, 'batch-job-def-secrets', {
333333
});
334334
```
335335

336+
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.
337+
336338
### Importing an existing Job Definition
337339

338340
#### From ARN

packages/@aws-cdk/aws-batch/lib/job-definition.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,11 +476,11 @@ export class JobDefinition extends Resource implements IJobDefinition {
476476
this.jobDefinitionName = this.getResourceNameAttribute(jobDef.ref);
477477
}
478478

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

482482
if (env === undefined) {
483-
return undefined;
483+
return vars;
484484
}
485485

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

522+
// If the AWS_*** environment variables are not explicitly set to the container, infer them from the current environment.
523+
// This makes the usage of tools like AWS SDK inside the container frictionless
524+
525+
const environment = this.deserializeEnvVariables(container.environment);
526+
527+
if (!environment.find((x) => x.name === 'AWS_REGION')) {
528+
environment.push({
529+
name: 'AWS_REGION',
530+
value: Stack.of(this).region,
531+
});
532+
}
533+
if (!environment.find((x) => x.name === 'AWS_ACCOUNT')) {
534+
environment.push({
535+
name: 'AWS_ACCOUNT',
536+
value: Stack.of(this).account,
537+
});
538+
}
539+
522540
return {
523541
command: container.command,
524-
environment: this.deserializeEnvVariables(container.environment),
542+
environment,
525543
secrets: container.secrets
526544
? Object.entries(container.secrets).map(([key, value]) => {
527545
return {
@@ -584,4 +602,4 @@ export class JobDefinition extends Resource implements IJobDefinition {
584602
};
585603
});
586604
}
587-
}
605+
}

packages/@aws-cdk/aws-batch/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"@aws-cdk/assertions": "0.0.0",
8484
"@aws-cdk/cdk-build-tools": "0.0.0",
8585
"@aws-cdk/integ-runner": "0.0.0",
86+
"@aws-cdk/integ-tests": "0.0.0",
8687
"@aws-cdk/cfn2ts": "0.0.0",
8788
"@aws-cdk/pkglint": "0.0.0",
8889
"@types/jest": "^27.5.2",

packages/@aws-cdk/aws-batch/test/batch.integ.snapshot/batch-stack.assets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"version": "20.0.0",
33
"files": {
4-
"d3685c79f9ec67f5dd6fda839a136b079f201b3d72695fe0ea3b3788c3471cc8": {
4+
"dd24d4801d80a639d9d7dcd67e4caa9af438a4af95a6243cdebbe188e79ba312": {
55
"source": {
66
"path": "batch-stack.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "d3685c79f9ec67f5dd6fda839a136b079f201b3d72695fe0ea3b3788c3471cc8.json",
12+
"objectKey": "dd24d4801d80a639d9d7dcd67e4caa9af438a4af95a6243cdebbe188e79ba312.json",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1414
}
1515
}

packages/@aws-cdk/aws-batch/test/batch.integ.snapshot/batch-stack.template.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,20 @@
16591659
"Properties": {
16601660
"Type": "container",
16611661
"ContainerProperties": {
1662+
"Environment": [
1663+
{
1664+
"Name": "AWS_REGION",
1665+
"Value": {
1666+
"Ref": "AWS::Region"
1667+
}
1668+
},
1669+
{
1670+
"Name": "AWS_ACCOUNT",
1671+
"Value": {
1672+
"Ref": "AWS::AccountId"
1673+
}
1674+
}
1675+
],
16621676
"Image": {
16631677
"Fn::Join": [
16641678
"",
@@ -1735,6 +1749,20 @@
17351749
"Properties": {
17361750
"Type": "container",
17371751
"ContainerProperties": {
1752+
"Environment": [
1753+
{
1754+
"Name": "AWS_REGION",
1755+
"Value": {
1756+
"Ref": "AWS::Region"
1757+
}
1758+
},
1759+
{
1760+
"Name": "AWS_ACCOUNT",
1761+
"Value": {
1762+
"Ref": "AWS::AccountId"
1763+
}
1764+
}
1765+
],
17381766
"Image": "docker/whalesay",
17391767
"Privileged": false,
17401768
"ReadonlyRootFilesystem": false,
@@ -1806,6 +1834,20 @@
18061834
"Properties": {
18071835
"Type": "container",
18081836
"ContainerProperties": {
1837+
"Environment": [
1838+
{
1839+
"Name": "AWS_REGION",
1840+
"Value": {
1841+
"Ref": "AWS::Region"
1842+
}
1843+
},
1844+
{
1845+
"Name": "AWS_ACCOUNT",
1846+
"Value": {
1847+
"Ref": "AWS::AccountId"
1848+
}
1849+
}
1850+
],
18091851
"ExecutionRoleArn": {
18101852
"Fn::GetAtt": [
18111853
"executionroleD9A39BE6",

packages/@aws-cdk/aws-batch/test/batch.integ.snapshot/manifest.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,36 @@
231231
"data": "batchspotcomputeenv2CE4DFD9"
232232
}
233233
],
234+
"/batch-stack/batch-demand-compute-env-launch-template-2/Resource-Security-Group/Resource": [
235+
{
236+
"type": "aws:cdk:logicalId",
237+
"data": "batchdemandcomputeenvlaunchtemplate2ResourceSecurityGroupBEA8DDD5"
238+
}
239+
],
240+
"/batch-stack/batch-demand-compute-env-launch-template-2/Ecs-Instance-Role/Resource": [
241+
{
242+
"type": "aws:cdk:logicalId",
243+
"data": "batchdemandcomputeenvlaunchtemplate2EcsInstanceRoleEE146754"
244+
}
245+
],
246+
"/batch-stack/batch-demand-compute-env-launch-template-2/Instance-Profile": [
247+
{
248+
"type": "aws:cdk:logicalId",
249+
"data": "batchdemandcomputeenvlaunchtemplate2InstanceProfileC5A36CBC"
250+
}
251+
],
252+
"/batch-stack/batch-demand-compute-env-launch-template-2/Resource-Service-Instance-Role/Resource": [
253+
{
254+
"type": "aws:cdk:logicalId",
255+
"data": "batchdemandcomputeenvlaunchtemplate2ResourceServiceInstanceRole41CADAC1"
256+
}
257+
],
258+
"/batch-stack/batch-demand-compute-env-launch-template-2/Resource": [
259+
{
260+
"type": "aws:cdk:logicalId",
261+
"data": "batchdemandcomputeenvlaunchtemplate2E12D5CBC"
262+
}
263+
],
234264
"/batch-stack/batch-job-queue/Resource": [
235265
{
236266
"type": "aws:cdk:logicalId",

0 commit comments

Comments
 (0)