- Più recenti
- Maggior numero di voti
- Maggior numero di commenti
Hi,
The only approach I am aware of is to programmatically use methods to import existing resources. For example, to import an existing bucket in cdk you could use this:
const importedBucket = s3.Bucket.fromBucketName( this, 'imported-bucket', 'BUCKET_NAME', );
Or use fromBucketArn. Similarly methods starting with “from” on different resources can be used.
Hope it helps ;)
In general, you can import existing resources provisioned manually into your CDK stack using the relevant AWS service modules. The process for importing a resource will vary depending on the service and resource type. However, the general steps are:
- Identify the resource you want to import and its unique identifier. This could be an ID, ARN, or other identifier depending on the resource type.
- Create a construct in your CDK stack that corresponds to the resource type. For example, if you want to import an
S3 bucket
, you would create anaws_s3.Bucket
construct. - Use the relevant method in the AWS service module to import the resource. This could be a
from_xxx_id
method, afrom_xxx_arn
method, or another method depending on the resource type. - Use the imported resource as a property value or reference in your CDK stack.
Here's an example of importing an existing S3 bucket:
from aws_cdk import core
from aws_cdk import aws_s3 as s3
class MyStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Import an existing S3 bucket
existing_bucket = s3.Bucket.from_bucket_name(self, "ExistingBucket",
bucket_name="my-existing-bucket"
)
# Use the imported bucket as a resource in your CDK stack
my_resource = my_module.MyResource(self, "MyResource",
some_property=existing_bucket.bucket_arn
)
In this example, the Bucket.from_bucket_name
method creates a Bucket construct from an existing S3 bucket with the specified bucket name. You can also import other resources, such as DynamoDB tables, Lambda functions, and more, using the relevant AWS service modules.
Once you've imported the resource, you can use its properties and methods as if it were defined in your CDK stack. In this example, the my_resource
construct uses the imported bucket's ARN as a property value.
Note that when you import an existing resource, you need to make sure that it's in the same AWS account and region as your CDK stack. You also need to have the necessary permissions to access and modify the resource.
Thanks for your in-depth response. As noted in a comment that I made in response to another answer, the “Stack Actions” / “Import resources into stack" as explained in the article is not the same kind of import as the
s3.Bucket.from_bucket_name
However, hopefully your response will help someone who is looking for that kind of information. Thanks for your time.
Contenuto pertinente
- AWS UFFICIALEAggiornata 2 anni fa
- AWS UFFICIALEAggiornata un anno fa
Kind of you to respond so promptly. As you may know, the “Stack Actions” / “Import resources into stack" as explained in the article is not the same kind of import as the fromBucket.
Looks like we will need to follow the manual steps as outlined in the article which includes going into the "AWS Console > CloudFormation" and doing the “Stack Actions” / “Import resources into stack" using the Cloudformation-template output of the
cdk synth
Thanks for your time.