hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.85k stars 9.19k forks source link

[Enhancement]: Lex v2 alias support #36044

Closed GRA0007 closed 8 months ago

GRA0007 commented 8 months ago

Description

This issue prompted support for initial Lex v2 resources, however as they currently stand, are not enough to fully create a Lex v2 bot using Terraform.

I am requesting a way to create bot aliases, including associating lambda functions with bot alias language support.

Additionally, I may be misunderstanding (the docs need to be fleshed out), but I don't believe there's currently any way to build the bot.

Finally, the documentation is also lacking on this regard, but I can't find a way to edit the preexisting FallbackIntent provided by AWS when creating a bot. Specifically, I'd like a way to enable the dialog_code_hook.

Affected Resource(s) and/or Data Source(s)

aws_lexv2models_bot_alias

Potential Terraform Configuration

resource "aws_lexv2models_bot_alias" "my_bot" {
  bot_id = aws_lexv2models_bot.my_bot.id
  bot_version = aws_lexv2models_bot_locale.my_bot.bot_version
  name = "ProdAlias"
  description = "Production alias"

  locale_settings = {
    "en_US" = {
      enabled = true
      code_hook_specification = {
        lambda_code_hook = {
          version = "$LATEST"
          lambda_arn = aws_lambda_function.my_handler.arn
        }
      }
    }
  }
}

References

API docs for creating an alias: https://docs.aws.amazon.com/lexv2/latest/APIReference/API_CreateBotAlias.html

Would you like to implement a fix?

No

github-actions[bot] commented 8 months ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

GRA0007 commented 8 months ago

For anyone landing here looking for how to fully create a Lex bot with Terraform, I've created a workaround using the AWS CLI until it's officially supported. Note that I tried using awscc_lex_bot_alias, but this had other issues, where it would hang if the alias already existed.

my_bot.tf

```tf data "aws_partition" "current" {} # IAM role for the bot resource "aws_iam_role" "my_bot" { name = "MyBotRole" assume_role_policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = "sts:AssumeRole" Effect = "Allow" Sid = "" Principal = { Service = "lexv2.amazonaws.com" } }, ] }) } resource "aws_iam_role_policy_attachment" "my_bot" { role = aws_iam_role.my_bot.name policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AmazonLexFullAccess" } resource "aws_lexv2models_bot" "my_bot" { name = "MyBot" idle_session_ttl_in_seconds = 1200 # 20 minutes role_arn = aws_iam_role.my_bot.arn data_privacy { child_directed = false } } resource "aws_lexv2models_bot_locale" "my_bot" { bot_id = aws_lexv2models_bot.my_bot.id bot_version = "DRAFT" locale_id = "en_US" n_lu_intent_confidence_threshold = 0.40 # Update default fallback to enable lambda hook provisioner "local-exec" { command = "aws lexv2-models update-intent --bot-id ${aws_lexv2models_bot.my_bot.id} --locale-id ${aws_lexv2models_bot_locale.my_bot.locale_id} --bot-version DRAFT --intent-id FALLBCKINT --intent-name FallbackIntent --parent-intent-signature=AMAZON.FallbackIntent --dialog-code-hook enabled=true --region ${var.aws_region}" } } resource "aws_lexv2models_intent" "my_bot" { bot_id = aws_lexv2models_bot.my_bot.id bot_version = aws_lexv2models_bot_locale.my_bot.bot_version locale_id = aws_lexv2models_bot_locale.my_bot.locale_id name = "HelloIntent" dialog_code_hook { enabled = true } sample_utterance { utterance = "Hello" } sample_utterance { utterance = "Howdy" } } # This builds the bot after creating an intent resource "null_resource" "my_bot_build" { depends_on = [aws_lexv2models_intent.my_bot] triggers = { bot_id = aws_lexv2models_bot.my_bot.id locale_id = aws_lexv2models_bot_locale.my_bot.locale_id intent = aws_lexv2models_intent.my_bot.sample_utterance } provisioner "local-exec" { command = "./build_bot.sh ${aws_lexv2models_bot.my_bot.id} ${aws_lexv2models_bot_locale.my_bot.locale_id}" } } # Create a version after building resource "aws_lexv2models_bot_version" "my_bot" { depends_on = [null_resource.my_bot_build] bot_id = aws_lexv2models_bot.my_bot.id locale_specification = { (aws_lexv2models_bot_locale.my_bot.locale_id) = { source_bot_version = aws_lexv2models_bot_locale.my_bot.bot_version } } } # Create or update an alias after creating a new version resource "null_resource" "my_bot_alias" { depends_on = [aws_lexv2models_bot_version.my_bot] triggers = { bot_id = aws_lexv2models_bot.my_bot.id locale_id = aws_lexv2models_bot_locale.my_bot.locale_id latest_version = aws_lexv2models_bot_version.my_bot.bot_version lambda_arn = example.lambda.arn logs_arn = example.cloudwatch.arn } provisioner "local-exec" { command = "./upsert_bot_alias.sh ${aws_lexv2models_bot.my_bot.id} ${aws_lexv2models_bot_locale.my_bot.locale_id} ${aws_lexv2models_bot_version.my_bot.bot_version} '${example.lambda.arn}' '${example.cloudwatch.arn}'" } } ```

And then, the two scripts that I use to build the bot, and upsert the bot alias:

build_bot.sh

```bash #!/bin/bash # Usage: ./build_bot.sh set -e echo "Building bot with ID $1" aws lexv2-models build-bot-locale --bot-id $1 --locale-id $2 --bot-version DRAFT # Wait for the build to finish while [ "$(aws lexv2-models describe-bot-locale --bot-id $1 --locale-id $2 --bot-version DRAFT --query botLocaleStatus --output text)" != "Built" ] do sleep 5 done ```

upsert_bot_alias.sh

```bash #!/bin/bash # Usage: ./upsert_bot_alias.sh set -e ALIAS_NAME="PublicAlias" # Check if the alias already exists CURRENT_ALIAS_ID=$(aws lexv2-models list-bot-aliases --bot-id $1 --query "botAliasSummaries[?botAliasName == '$ALIAS_NAME'].botAliasId | [0]" --output text) if [[ $CURRENT_ALIAS_ID == "None" ]]; then echo "Creating a new alias" CURRENT_ALIAS_ID=$(aws lexv2-models create-bot-alias --bot-id $1 --bot-alias-name $ALIAS_NAME --query botAliasId --output text) fi echo "Updating the alias" # Enable lambda execution and chat logs aws lexv2-models update-bot-alias --bot-id $1 --bot-alias-id $CURRENT_ALIAS_ID --bot-alias-name $ALIAS_NAME --bot-version $3 --bot-alias-locale-settings '{"'$2'":{"enabled":true,"codeHookSpecification":{"lambdaCodeHook":{"lambdaARN":"'$4'","codeHookInterfaceVersion":"1.0"}}}}' --conversation-log-settings '{"textLogSettings":[{"enabled":true,"destination":{"cloudWatch":{"cloudWatchLogGroupArn":"'$5'","logPrefix":""}}}]}' ```

In this example you can see I am enabling chat logs and associating a lambda function with my bot alias as well.

justinretzolk commented 8 months ago

Hey @GRA0007 👋 Thank you for taking the time to raise this! It looks like one of our engineers has already opened an issue tracking the need to add this to the provider, so I'm going to close this one as a duplicate. If you'd like to be notified of any further actions, I'd recommend keeping an eye on that issue.

justinretzolk commented 8 months ago

Duplicate of #35780

github-actions[bot] commented 7 months ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.