FROM public.ecr.aws/lambda/ruby:3.2 AS deploy

# Update and install prerequisites for Datadog Lambda extension
RUN yum -y update \
    && yum -y install gcc gcc-c++ gmp-devel make \
    && yum -y clean all

COPY --from=public.ecr.aws/datadog/lambda-extension:44 /opt/extensions/ /opt/extensions

# Install dependencies
COPY Gemfile Gemfile.lock ./
RUN bundle config set deployment true
RUN bundle config set path 'vendor/bundle'
RUN bundle config set without 'test'
RUN bundle install

# Add application code
COPY lib ./lib

EXPOSE 8080

CMD ["lib/app.LambdaFunction::Handler.process"]

FROM deploy AS test

# Install test dependencies
RUN bundle config unset without
RUN bundle install

# Add tests
COPY .rubocop.yml ./
COPY lint-and-test.sh ./
COPY spec ./spec

ENTRYPOINT ["bash"]
CMD ["./lint-and-test.sh"]
