Testing Terraform with AWS Emulator
78 points by meysamazad a day ago | 14 comments
  • 0xbadcafebee a day ago |
    The problem with testing with Terraform or any other tool is that the emulator won't accurately reflect real-world behavior. The real world services have a bunch of logic and code of them which isn't all replicated in the emulator. The only thing the emulator does is replicate the API. It's a bit like trying to build a model of a house by building the model to look like the outside of the house. Gives you an idea what it looks like, but working within that model is not going to be the same as working within the actual house.
    • ozarkerD a day ago |
      Agreed. We also have a fair amount of TF testing involving IAM to make sure we get permissions right which the emulator doesn’t emulate at all unfortunately.
    • QGQBGdeZREunxLe a day ago |
      The problem with full e2e tests for Terraform is that they are 1. Extremely slow 2. Can be expensive.

      LocalStack and hopefully the new AWS Emulator can give you a fairly high degree of confidence.

    • ctkhn a day ago |
      Most teams I've been on have used a pre-production environment for a combination of performance testing and making sure infra worked as expected. To a smaller degree with less instances etc, we use our dev and test environments to make sure infra works the same as it should in higher envs.
    • OJFord a day ago |
      Let's not do unit tests or local dev environments either then.
    • jamesfinlayson a day ago |
      Yep. Twice this year I've had working CloudFormation break because AWS decided to change validation without telling us.
  • whinvik a day ago |
    Nice. Now for GCP to also release such a tool
  • itintheory a day ago |
    > These mock calls can't know for a fact that you already have a resource in AWS called "my-bucket-asdasdasd"

    Doesn't seem like the AWS Emulator would know that either, unless it's connecting to real AWS... Also, bucket names are global, so it doesn't matter what you have, it's what anyone has.

  • danw1979 16 hours ago |
    PSA: don’t ever pass your AWS credentials as TF_VAR_s, they’ll end up in the state file, which is a bad thing.

    from the article, in a github actions workflow:

    ‘TF_VAR_secret_key: … ‘

    and yeah this was just for the “emulator” provider config but you know someone will come along and copy your example for a real workflow and provider config.

    (Yes, I know about state encryption, thank you.)

  • lmz 14 hours ago |
    Ok, but how does this replicate the current/before state from the live environment? Wouldn't this always do a from-scratch creation?
    • hatedabamboo 13 hours ago |
      Yes, every run inside pipeline starts from scratch, Emulator state is in-memory only.