Cross Stack Dependencies Between Nested Stack [Stack 2 depend on Exported value from Stack 1]

0

How do i resolve Dependencies form stack 1 in stack 2 Nested Stack Based on Imported Value form Stack 1

But i keep getting Error

# Stack 1: Create S3 bucket and export its name
Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    ...
  ExportBucketName:
    Value: !Ref MyBucket
    Export:
      Name: MyBucketName

# Stack 2: Create EC2 instance and reference S3 bucket
Resources:
  MyInstance:
    Type: AWS::EC2::Instance
    DependsOn: !ImportValue MyBucketName
    Properties:
      ...
      S3BucketName: !ImportValue MyBucketName
Nafiu
질문됨 9달 전443회 조회
1개 답변
1
수락된 답변

In CloudFormation, the DependsOn attribute is used to specify that the creation of a specific resource follows another. When it comes to !ImportValue, it fetches an exported value from another stack, but you can't use !ImportValue in DependsOn like you tried to do because it doesn't represent a resource but a value.

The dependency you're trying to establish doesn't make sense in the context of CloudFormation. The creation of an EC2 instance doesn't directly depend on the existence of an S3 bucket. It depends on the availability of the S3 bucket's name, which, once the S3 bucket is created and its name is exported, will always be available for import in another stack.

If you want to ensure that Stack 2 (the EC2 instance) is only deployed after Stack 1 (the S3 bucket) is successfully deployed, you need to control the deployment order outside of the CloudFormation templates themselves. This could be through a CI/CD pipeline, scripts, or manual deployment.

Your Stack 2 can be written like:

Resources: MyInstance: Type: AWS::EC2::Instance Properties: ... S3BucketName: !ImportValue MyBucketName

You don't need DependsOn: !ImportValue MyBucketName because there's no resource in Stack 2 that corresponds to the imported S3 bucket. As long as you ensure that Stack 1 is deployed before Stack 2, the value will be available for import.

To avoid errors:

  1. Ensure that Stack 1 is deployed and has successfully created the S3 bucket.
  2. Make sure that the export name in Stack 1 (MyBucketName) matches the import name in Stack 2.
  3. Only after Stack 1 is successfully deployed, deploy Stack 2.

This will make sure that when Stack 2 is deployed, it will have the S3 bucket name available for import.

답변함 9달 전
profile pictureAWS
전문가
검토됨 9달 전
profile picture
전문가
검토됨 9달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠