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

(opensearch): Trying to define an Opensearch domain in one stack and reference it in another causes cyclic reference errors #20256

Open
SamStephens opened this issue May 9, 2022 · 6 comments
Assignees
Labels
@aws-cdk/aws-opensearch Related to the @aws-cdk/aws-opensearchservice package bug This issue is a bug. p3

Comments

@SamStephens
Copy link
Contributor

SamStephens commented May 9, 2022

Describe the bug

I want to use the classic pattern where you have an infrastructure stack and a code stack.

I define an Opensearch domain in my infrastructure stack in a VPC. I then define a code stack, and attempt to wire up a Lambda that uses that domain.

This works when all the resources are in one stack; or if the code stack is nested in the infrastructure stack. But if they're peer stacks I get a cyclic reference stack.

For the reproduction provided below, the error is:

'InfrastructureStack' depends on 'CodeStack' (InfrastructureStack -> CodeStack/LambdaFunction/SecurityGroup/Resource.GroupId). Adding this dependency (CodeStack -> InfrastructureStack/OpensearchDomain/Resource.Arn) would create a cyclic reference.

Expected Behavior

For all the resources related to my Lambda to be defined in the Code stack, so there's no cyclic references. That's what I'd do if I was writing the Cloudformation directly.

Current Behavior

The lambda function itself is defined in the code stack Cloudformation. However, the AWS::EC2::SecurityGroupIngress allowing the lambda function ingress to the Opensearch domain is defined in the infrastructure stack Cloudformation.

Reproduction Steps

The simplest reproduction I could come up with is to generate a new python application using cdk init app --language python, and then update app.py to be:

#!/usr/bin/env python3

import aws_cdk as cdk
from aws_cdk import aws_opensearchservice, aws_lambda, aws_ec2
from constructs import Construct


class InfrastructureStack(cdk.Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        self._vpc = aws_ec2.Vpc(
            scope=self,
            id="Vpc",
            cidr="10.64.0.0/16",
        )

        self._domain = aws_opensearchservice.Domain(
            scope=self,
            id="OpensearchDomain",
            version=aws_opensearchservice.EngineVersion.OPENSEARCH_1_2,
            vpc=self._vpc,
        )

    @property
    def domain(self) -> aws_opensearchservice.Domain:
        return self._domain

    @property
    def vpc(self) -> aws_ec2.IVpc:
        return self._vpc


class CodeStack(cdk.Stack):
    def __init__(self,
                 scope: Construct,
                 construct_id: str,
                 domain: aws_opensearchservice.Domain,
                 vpc: aws_ec2.IVpc,
                 **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        self._lambda_function = aws_lambda.Function(
            scope=self,
            id="LambdaFunction",
            handler="index.handler",
            runtime=aws_lambda.Runtime.PYTHON_3_9,
            code=aws_lambda.Code.from_inline("handler code goes here"),
            environment={
                "OPENSEARCH_ENDPOINT": domain.domain_endpoint,
            },
            vpc=vpc,
        )

        domain.connections.allow_from(self._lambda_function, aws_ec2.Port.tcp(443))
        domain.grant_read_write(self._lambda_function)


app = cdk.App()

infrastructure_stack = InfrastructureStack(app, "InfrastructureStack")
CodeStack(app, "CodeStack", infrastructure_stack.domain, infrastructure_stack.vpc)

app.synth()

Possible Solution

For all the resources related to my Lambda to be defined in the Code stack, so there's no cyclic references. That's what I'd do if I was writing the Cloudformation directly.

Additional Information/Context

No response

CDK CLI Version

2.23.0 (build 50444aa)

Framework Version

2.24.0

Node.js Version

14.17.0

OS

Ubuntu (Windows Subsystem for Linux)

Language

Python

Language Version

3.9.7

Other information

No response

@SamStephens SamStephens added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels May 9, 2022
@github-actions github-actions bot added the @aws-cdk/aws-opensearch Related to the @aws-cdk/aws-opensearchservice package label May 9, 2022
@kaizencc
Copy link
Contributor

Hi @SamStephens! Can you provide the cyclic dependency error that you're getting as well? Interested in understanding exactly what resources CDK thinks depend on each other. Usually with these dependencies there's some CDK magic under the hood that you never need to know about until you hit these dependency errors :), but I can dive into the source code for you and see whats up.

@kaizencc kaizencc added needs-reproduction This issue needs reproduction. and removed needs-triage This issue or PR still needs to be triaged. labels May 10, 2022
@kaizencc kaizencc changed the title (aws_opensearch): Trying to define an Opensearch domain in one stack and reference it in another causes cyclic reference errors (opensearch): Trying to define an Opensearch domain in one stack and reference it in another causes cyclic reference errors May 10, 2022
@kaizencc kaizencc added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label May 10, 2022
@SamStephens
Copy link
Contributor Author

@kaizencc Sure thing; I'll put together a proper repro in a few days time when I'm back at work.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label May 10, 2022
@SamStephens
Copy link
Contributor Author

@kaizencc also I was interested in asking about broader design goals here. Is the infrastructure stack/code stack split pattern something that the CDK is explicitly interested in supporting?

Basically, my situation is that I'm currently exporting ARNs from my infrastructure stack and importing the resources using them into my code stack; this is the approach I'm seeing others to work around similar issues. Being able to directly share the resources between the two stacks would be cleaner; but I don't want to do it and then find myself with an issue that is considered by design and will not be resolved. I want to know that the CDK considers this is a usage pattern it supports before I move to it.

@kaizencc
Copy link
Contributor

@SamStephens regarding your second question, I think you'd be better off opening a new guidance issue about it so that the question gets into the hands of the right people. My answer is that I'm not entirely sure. How are you planning on sharing resources between two stacks? I think if your plan is to use nested stacks to achieve this then that is definitely a CDK pattern we plan to support.

@SamStephens
Copy link
Contributor Author

Thanks @kaizencc. I've updated the bug report to include a proper reproduction.

I'll open a separate guidance issue about the pattern, as you suggest. However, I am interested in using peer stacks as per the reproduction, not nested stacks. When I worked with Nested Stacks back when I was an AWS employee I found them quite clunky to work with, although this was a few years ago. I'd rather operate using peer stacks with exported using CfnOutput and imported using Fn.import_value, which is my current workaround for this issue.

@SamStephens
Copy link
Contributor Author

@kaizencc I've got a proper repro here now, can you please remove the needs-reproduction tag?

@khushail khushail added p2 and removed needs-reproduction This issue needs reproduction. labels Mar 15, 2023
@pahud pahud added p3 and removed p2 labels Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-opensearch Related to the @aws-cdk/aws-opensearchservice package bug This issue is a bug. p3
Projects
None yet
Development

No branches or pull requests

4 participants