5

I am using Terraform script to create aws elastic beanstalk environment, I need to launch a shell script on instance launch

I have already tried the following

resource "aws_elastic_beanstalk_environment" "Environment" {
    name = "${var.ebs_env_name}"
    application = "${var.ebs_app_name}"
    ---
    ---
    ---
    setting = {
      namespace = "aws:autoscaling:launchconfiguration"
      name = "user_data"
      value = "${file("user-data.sh")}"
   }
}

This is throwing error

Error applying plan:

1 error(s) occurred:

aws_elastic_beanstalk_environment.Environment: ConfigurationValidationException: Configuration validation exception: Invalid option specification (Namespace: 'aws:autoscaling:launchconfiguration', OptionName: 'user_data'): Unknown configuration setting. status code: 400, request id: xxxxx-xxxxxx

Please Help

Bikesh M
  • 8,163
  • 6
  • 40
  • 52

2 Answers2

9

Thanks for the Answer, I found a solution

I have created a folder .ebextensions and created a file inside the folder called 99delayed_job.config (you can give any name)

enter image description here

commands: 
  create_post_dir: 
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/pre"
    ignoreErrors: true
files: 
  /opt/elasticbeanstalk/hooks/appdeploy/pre/99_restart_delayed_job.sh: 
    group: root
    mode: "000755"
    owner: root
    content: |-
        #!/usr/bin/env bash
        <My shell script here>

A and zipped with 'Dockerrun.aws.json' this zip I am sending to s3 and used to deploy

enter image description here

Working fine :)

Brian
  • 12,145
  • 20
  • 90
  • 153
Bikesh M
  • 8,163
  • 6
  • 40
  • 52
  • What if you want to run a long living task without blocking the deployment. In my case after trying you suggestion the eb deployer is stack in this step and never change the node status to green – mspapant Mar 11 '20 at 10:41
  • 2
    !! WARNING !! "Custom platform hooks are a legacy feature that exists on Amazon Linux AMI platforms. On Amazon Linux 2 platforms, custom platform hooks in the /opt/elasticbeanstalk/hooks/ folder are entirely discontinued. Elastic Beanstalk doesn't read or execute them. Amazon Linux 2 platforms support a new kind of platform hooks, specifically designed to extend Elastic Beanstalk managed platforms." - https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/custom-platform-hooks.html – Zachary Ryan Smith Jul 31 '21 at 12:16
3

I couldn't find any information on the AWS Elastic Beanstalk service exposing a means to modify the user_data on the instances. You can however adjust the AMI used, so you could use a tool like Packer to build yourself a custom AMI that includes the user_data in it.

catsby
  • 11,276
  • 3
  • 37
  • 37