# Data source in CDKTF

**URL:** <https://discuss.hashicorp.com/t/data-source-in-cdktf/39817>\
**Category:** Terraform\
**Created:** [May 21, 2022, 7:25pm UTC](https://discuss.hashicorp.com/t/data-source-in-cdktf/39817 "2022-05-21T19:25:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkm29](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/mkm29/32/14287_2.png) [@mkm29](https://discuss.hashicorp.com/u/mkm29)\
**Post date:** [May 21, 2022, 7:25pm UTC](https://discuss.hashicorp.com/t/data-source-in-cdktf/39817/1 "2022-05-21T19:25:32Z")

</div>

Hi, I am trying to replicate one of my simple stacks from HCL to TypeScript (using CDKTF), I am having issues setting up a data source to get a list of AMIs. This is what I am trying to do:

```auto
  const amis = new ec2.DataAwsAmiIds(this, "ami-ids", {
    owners: ["amazon"],
    nameRegex: "^amzn2-ami-hvm-*-x86_64-ebs",
    filter: [{
      name: "name",
      values: ["amzn2-ami-hvm-*-x86_64-ebs"]
    }]
  });

```

I then try to reference this like so:

```auto
const instance = new ec2.Instance(this, "instance", {
  instanceType: "t2.micro",
  ami: `${amis.id}`,
  ...

```

The issue I am having is that this value will be a Token (`${TfToken[TOKEN.1]}`), how to I get the string value from this in order to be able to pass it in? Thanks.

The same thing applies to generating a key pair using the TLS provider, here is the complete [source code](https://github.com/mkm29/cdktf-ec2/blob/main/main.ts).

---

<div class="post-metadata">

**Author:** ![mkm29](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/mkm29/32/14287_2.png) [@mkm29](https://discuss.hashicorp.com/u/mkm29)\
**Post date:** [May 22, 2022, 9:28pm UTC](https://discuss.hashicorp.com/t/data-source-in-cdktf/39817/2 "2022-05-22T21:28:31Z")

</div>

I changed `ami: ...` to `ami: Fn.element(amis.ids, 0)` which resolved the AMI issue. However, I am experiencing a similar issue when creating the private key and writing to a local file, not sure how to do this in CDKTF, in HCL I just specify the content in a `local_sensitive_file` block. How do I do this in CDKTF?
