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: look up AWS managed Prefix Lists #15115

Closed
1 of 2 tasks
alanraison opened this issue Jun 14, 2021 · 45 comments · Fixed by #33619
Closed
1 of 2 tasks

ec2: look up AWS managed Prefix Lists #15115

alanraison opened this issue Jun 14, 2021 · 45 comments · Fixed by #33619
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2

Comments

@alanraison
Copy link
Contributor

alanraison commented Jun 14, 2021

There is currently no way to lookup the IP of an AWS-managed Prefix List (i.e. those for S3 and DynamoDB).

Use Case

In order to use an S3 or DynamoDB Gateway endpoint, with a Security Group which allows only specific outbound access, it is necessary to lookup the com.amazonaws.<region>.s3 or com.amazonaws.<region>.dynamodb Prefix List's ID. This is currently not possible.

Proposed Solution

Add the ability to look up a Prefix List by prefix list ID. I don't know if this requires changes in Cloudformation.

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

This is a 🚀 Feature Request

@alanraison alanraison added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Jun 14, 2021
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Jun 14, 2021
@alanraison alanraison changed the title (ec2): look up AWS managed Prefix Lists ec2: look up AWS managed Prefix Lists Jun 14, 2021
@njlynch njlynch added effort/medium Medium work item – several days of effort p2 and removed needs-triage This issue or PR still needs to be triaged. labels Jun 22, 2021
@njlynch njlynch removed their assignment Jun 22, 2021
@njlynch
Copy link
Contributor

njlynch commented Jun 22, 2021

There is no current CloudFormation support for this. The guidance I have seen elsewhere is to use a Custom Resource to do the lookup. See the description of #13668 for one example of doing that lookup via a Custom Resource.

This is something we could conceivably integrate into the CDK, but it's not clear yet how broad an impact it would have or where it would live. I am unassigning and marking this issue as p2, which means that we are unable to work on this immediately.

We use +1s to help prioritize our work, and are happy to revaluate this issue based on community feedback. You can reach out to the cdk.dev community on Slack to solicit support for reprioritization. In the meantime, I hope the linked workaround helps!

@ramiro
Copy link

ramiro commented Oct 21, 2021

See also #9568.

@markoperich
Copy link

+1

2 similar comments
@havenith
Copy link

havenith commented Dec 3, 2021

+1

@gottschalkj-fmr
Copy link

+1

@github-actions github-actions bot added p1 and removed p2 labels Jul 7, 2022
@github-actions
Copy link

github-actions bot commented Jul 7, 2022

This issue has received a significant amount of attention so we are automatically upgrading its priority. A member of the community will see the re-prioritization and provide an update on the issue.

@comcalvi comcalvi assigned comcalvi and unassigned comcalvi Jul 12, 2022
@falkvonohlen
Copy link

+1

4 similar comments
@igormukhin
Copy link

+1

@tisauro
Copy link

tisauro commented Sep 7, 2022

+1

@elginlam-amazon
Copy link

+1

@markrekveld
Copy link

+1

@MrArnoldPalmer MrArnoldPalmer added p2 and removed p1 labels Jan 27, 2023
@dennisschaaf
Copy link

+1

@mvs5465
Copy link

mvs5465 commented Mar 9, 2023

Does anyone know if you can reference prefix lists using CfnResourceShare?

@acatala-sistrol
Copy link

+1

1 similar comment
@antoniordz96
Copy link

+1

@mrgrain
Copy link
Contributor

mrgrain commented Jul 14, 2023

Region fact is fine, but I think we should start adding these kind of slowly (never) changing automated look-ups things into
https://github.com/cdklabs/awscdk-service-spec so it can be re-used here.

@bericp1
Copy link

bericp1 commented Jul 27, 2023

+1

@cogwirrel
Copy link
Member

As a workaround until this is implemented properly, I used AwsCustomResource to achieve the same result. Sharing it here in case it's useful :)

import { IPrefixList, PrefixList } from 'aws-cdk-lib/aws-ec2';
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { AwsCustomResource, AwsCustomResourcePolicy, PhysicalResourceId } from 'aws-cdk-lib/custom-resources';
import { Construct } from 'constructs';

export interface AwsManagedPrefixListProps {
  /**
   * Name of the aws managed prefix list.
   * See: https://docs.aws.amazon.com/vpc/latest/userguide/working-with-aws-managed-prefix-lists.html#available-aws-managed-prefix-lists
   * eg. com.amazonaws.global.cloudfront.origin-facing
   */
  readonly name: string;
}

export class AwsManagedPrefixList extends Construct {
  public readonly prefixList: IPrefixList;

  constructor(scope: Construct, id: string, { name }: AwsManagedPrefixListProps) {
    super(scope, id);

    const prefixListId = new AwsCustomResource(this, 'GetPrefixListId', {
      onUpdate: {
        service: '@aws-sdk/client-ec2',
        action: 'DescribeManagedPrefixListsCommand',
        parameters: {
          Filters: [
            {
              Name: 'prefix-list-name',
              Values: [name],
            },
          ],
        },
        physicalResourceId: PhysicalResourceId.of(`${id}-${this.node.addr.slice(0, 16)}`),
      },
      policy: AwsCustomResourcePolicy.fromStatements([
        new PolicyStatement({
          effect: Effect.ALLOW,
          actions: ['ec2:DescribeManagedPrefixLists'],
          resources: ['*'],
        }),
      ]),
    }).getResponseField('PrefixLists.0.PrefixListId');

    this.prefixList = PrefixList.fromPrefixListId(this, 'PrefixList', prefixListId);
  }
}

With example usage:

const cfOriginFacingPrefixList = new AwsManagedPrefixList(this, 'CloudfrontOriginPrefixList', {
  name: 'com.amazonaws.global.cloudfront.origin-facing',
}).prefixList;

@danib-ntt-sky
Copy link

+1

@werebear73-tritelph
Copy link

+1 - the workaround doesn't work on .NET (see above mention)

@ggzik-copperleaf
Copy link

+1, We ran into the same issue (also using cdk in dotnet) and used the Custom Resource solution as well:

private string GetPrefixListId(string stackId, IEnvironment env, string prefixListName)
{
    var customResourceName = $"{stackId}-GetPrefixListId";
    return new AwsCustomResource(this, "GetPrefixListId", new AwsCustomResourceProps {
        FunctionName = customResourceName,
        LogRetention = RetentionDays.ONE_DAY,
        OnUpdate = new AwsSdkCall {
            Service = "@aws-sdk/client-ec2",
            Action = "DescribeManagedPrefixListsCommand",
            Parameters = new Dictionary<string, object> {
                {
                    "Filters", new Dictionary<string, object>[] {
                        new Dictionary<string, object> {
                            { "Name", "prefix-list-name" },
                            { "Values", new string[] { prefixListName } }
                        }
                    }
                },
            },
            PhysicalResourceId = PhysicalResourceId.Of($"{stackId}-{Node.Addr.Substring(0, 16)}"),
        },
        Policy = AwsCustomResourcePolicy.FromStatements(new[] {
            new PolicyStatement(new PolicyStatementProps {
                Effect = Effect.ALLOW,
                Actions = new[] { "ec2:DescribeManagedPrefixLists" },
                Resources = new[] { "*" }, // ec2:DescribeManagedPrefixLists must be executed against resource *
                Conditions = new Dictionary<string, object> {{
                    "StringEquals", new Dictionary<string, string> {
                        { "aws:PrincipalAccount", env.Account },
                        { "aws:RequestedRegion", env.Region }
                    }
                }}
            }),
        }),
    }).GetResponseField("PrefixLists.0.PrefixListId");
}

Note that the policy has to use Resource: * because that is the only resource ec2:DescribeManagedPrefixLists, so to make it a bit more secure we added conditions to scope the action down to within our account.

@zvonimir-bednarcik
Copy link

+1

1 similar comment
@isunli
Copy link

isunli commented Feb 26, 2024

+1

@alexbaileymembr
Copy link

We are going to implement the workaround above but have a slightly different use case of needing to lookup prefix lists by name. It's only ID available at the moment which varies between accounts, regions etc. It would be good to know if a FromLookup or FromName option at Synth time is the way to go. If it is, then I'd feel more comfortable taking a stab at this.

@carlo-vassallo
Copy link

+1

2 similar comments
@epytka
Copy link

epytka commented Aug 8, 2024

+1

@vasylherman
Copy link

+1

@darmen
Copy link

darmen commented Sep 14, 2024

+1

Hello, I wonder if there's any progress on this feature request?

@madhan987
Copy link

+1

4 similar comments
@jinjiaKarl
Copy link

+1

@petexchai
Copy link

+1

@khaitranhq
Copy link

+1

@ylka
Copy link

ylka commented Dec 19, 2024

+1

@bericp1
Copy link

bericp1 commented Feb 1, 2025

Until there's a first-party solution, here's the custom resource I'm using to lookup the AWS VPC Lattice managed prefix lists that VPC lattice creates in the account when you create your first CfnServiceNetworkVpcAssociation in the account:

https://gist.github.com/bericp1/eb0ce72079161f45f4867a9e3ab02bd9

In case it helps anyone.

@asaid97
Copy link

asaid97 commented Feb 4, 2025

+1

1 similar comment
@devnull
Copy link

devnull commented Feb 14, 2025

+1

Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

1 similar comment
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 24, 2025
shikha372 pushed a commit to shikha372/aws-cdk that referenced this issue Apr 4, 2025
### Issue # (if applicable)

Closes aws#33606.
Closes aws#15115.

### Reason for this change

AWS-managed prefix lists are useful to control traffic VPC and AWS managed services.
The name of the AWS-managed prefix list is documented but the id should be copy&paste by hand.

### Description of changes

This PR implements `PrefixList.fromLookup()` to look up an existing managed prefix list by name.
``` ts
ec2.PrefixList.fromLookup(this, 'CloudFrontOriginFacing', {
  prefixListName: 'com.amazonaws.global.cloudfront.origin-facing',
});
```

Uses the new CloudControl context provider: aws/aws-cdk-cli#138 and cdklabs/cloud-assembly-schema#124.

### Describe any new or updated permissions being added

Nothing.

### Description of how you validated changes

Added unit tests and an integ test.

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.