Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EC2] make GenericMachineImage use CfnMapping in an agnostic stack #8759

Closed
2 tasks
vgribok opened this issue Jun 26, 2020 · 2 comments · Fixed by #12546
Closed
2 tasks

[EC2] make GenericMachineImage use CfnMapping in an agnostic stack #8759

vgribok opened this issue Jun 26, 2020 · 2 comments · Fixed by #12546
Assignees
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. good first issue Related to contributions. See CONTRIBUTING.md p2

Comments

@vgribok
Copy link

vgribok commented Jun 26, 2020

CDK demands AWS Region to be known at synth time when resolving IMachineImage for the EC2 Instance class, seemingly making it impossible to create a region-agnostic stack for launching an EC2 instance, while it's perfectly possible to defer region resolution in plain vanilla CFN to the run/deploy time.

Use Case

I need a stack that can launch an EC2 in any region. CFN allows to do that via a Mapping that maps AMI Id to a region.

Consider this CFN snippet:

Mappings:
  RegionMap:
    us-east-1: 
      AmiId: ami-XXXXXXXXXXX
    us-west-2: 
      AmiId: ami-YYYYYYYYYYY
...

Resources:
  ...
  InstanceMachine:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t3a.large
      ImageId: !FindInMap
        - RegionMap
        - !Ref 'AWS::Region'
        - AmiId

This shows how specifying instance AMI Id can be made region-agnostic.

When attempting to do this in CDK:

IMachineImage windowsAmi = MachineImage.GenericWindows(
    new Dictionary<string, string>
    { 
        ["us-east-1"] = "ami-XXXXXXXXXXX",
        ["eu-west-2"] = "ami-YYYYYYYYYYYY",
    }
);

var ec2Instance = new Instance_(this, "Instance", new InstanceProps {
    Vpc = vpc,
    InstanceType = InstanceType.Of(InstanceClass.BURSTABLE3_AMD, InstanceSize.XLARGE),
    MachineImage = windowsAmi,
    SecurityGroup = mySecurityGroup
});

... I am getting "Amazon.JSII.Runtime.JsiiException: Unable to determine AMI from AMI map since stack is region-agnostic" error.

Proposed Solution

Instead of throwing an exception when AWS region is not specified at synth time, use region-AMI dictionary that CDK already requires to generate CFN Mapping, outputting the Mapping into generated template, so that whatever the Aws.REGION is at run time, that region value could be used.

Other

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

@vgribok vgribok added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Jun 26, 2020
@SomayaB SomayaB changed the title [Amazon.CDK.AWS.EC2] EC2 MachineImage in CDK is not region-agnostic, while in CFN this use case is supported [EC2] EC2 MachineImage in CDK is not region-agnostic, while in CFN this use case is supported Jun 29, 2020
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Jun 29, 2020
@rix0rrr
Copy link
Contributor

rix0rrr commented Jul 8, 2020

You are right, that change shouldn't be too hard.

@rix0rrr rix0rrr added effort/small Small work item – less than a day of effort good first issue Related to contributions. See CONTRIBUTING.md labels Jul 8, 2020
@rix0rrr rix0rrr changed the title [EC2] EC2 MachineImage in CDK is not region-agnostic, while in CFN this use case is supported [EC2] make GenericMachineImage use CfnMapping in an agnostic stack Jul 8, 2020
@SomayaB SomayaB removed the needs-triage This issue or PR still needs to be triaged. label Jul 8, 2020
@rix0rrr rix0rrr added the p2 label Aug 12, 2020
wchaws added a commit to wchaws/aws-cdk that referenced this issue Jan 16, 2021
wchaws added a commit to wchaws/aws-cdk that referenced this issue Jan 16, 2021
wchaws added a commit to wchaws/aws-cdk that referenced this issue Jan 17, 2021
wchaws added a commit to wchaws/aws-cdk that referenced this issue Jan 17, 2021
@mergify mergify bot closed this as completed in #12546 Feb 9, 2021
mergify bot pushed a commit that referenced this issue Feb 9, 2021
…-agnostic stacks (#12546)

Close #8759


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

github-actions bot commented Feb 9, 2021

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

TLadd pushed a commit to TLadd/aws-cdk that referenced this issue Feb 9, 2021
…-agnostic stacks (aws#12546)

Close aws#8759


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
NovakGu pushed a commit to NovakGu/aws-cdk that referenced this issue Feb 18, 2021
…-agnostic stacks (aws#12546)

Close aws#8759


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. good first issue Related to contributions. See CONTRIBUTING.md p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants