micronaut-projects / micronaut-aws

Projects specific to integrating Micronaut and Amazon Web Services (AWS)
Apache License 2.0
87 stars 80 forks source link

Disabling environment deduction does not work with Lambda #1171

Open sdelamo opened 3 years ago

sdelamo commented 3 years ago

Steps To Reproduce

Create a Lambda function with the following environment variables:

Expected Behavior

I expected environment to be just ec2

Actual Behaviour

environments are ec2 cloud function lambda

Version

3.0.0

Workaround

A workaround is to create a class such as:

package example.micronaut;

package com.softamo.exchangeratesbot.function;

import io.micronaut.context.ApplicationContextBuilder;
import io.micronaut.context.DefaultApplicationContextBuilder;
import io.micronaut.context.env.Environment;
import io.micronaut.core.util.StringUtils;
import io.micronaut.function.aws.MicronautLambdaContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;

public class LambdaApplicationContextBuilderReplacement extends DefaultApplicationContextBuilder {
    private static final Boolean DEDUCE_ENVIRONMENT_DEFAULT = true;

    private static final Logger LOG = LoggerFactory.getLogger(LambdaApplicationContextBuilderReplacement.class);
    public LambdaApplicationContextBuilderReplacement() {
        setLambdaConfiguration(this);
    }

    public static void setLambdaConfiguration(ApplicationContextBuilder builder) {
        if (shouldDeduceEnvironment(builder)) {
            builder = builder.environments(Environment.FUNCTION, MicronautLambdaContext.ENVIRONMENT_LAMBDA);
        }
        builder.eagerInitConfiguration(true)
                .eagerInitSingletons(true);
    }

    public static boolean shouldDeduceEnvironment(ApplicationContextBuilder applicationContextBuilder) {
        if (applicationContextBuilder instanceof DefaultApplicationContextBuilder) {
            Optional<Boolean> deduceEnvironments = ((DefaultApplicationContextBuilder) applicationContextBuilder).getDeduceEnvironments();
            if (deduceEnvironments.isPresent()) {
                return deduceEnvironments.get();
            }
        }
        String deduceEnv = System.getenv(Environment.DEDUCE_ENVIRONMENT_ENV);
        if (StringUtils.isNotEmpty(deduceEnv)) {
            boolean deduce = Boolean.parseBoolean(deduceEnv);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Environment deduction was set via environment variable to: " + deduce);
            }
            return deduce;
        }
        String deduceProperty = System.getProperty(Environment.DEDUCE_ENVIRONMENT_PROPERTY);
        if (StringUtils.isNotEmpty(deduceProperty)) {
            boolean deduce = Boolean.parseBoolean(deduceProperty);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Environment deduction was set via system property to: " + deduce);
            }
            return deduce;
        }
        return DEDUCE_ENVIRONMENT_DEFAULT;
    }
}

and in the handler override MicronautRequestHandler::newApplicationContextBuilder

@Introspected
public class Handler extends MicronautRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
...
..
.    @Override
    protected ApplicationContextBuilder newApplicationContextBuilder() {
        return new LambdaApplicationContextBuilderReplacement();
    }
...
..
.
sdelamo commented 3 years ago

If micronaut-projects/micronaut-core#6077 gets merged, I plan to change LambdaApplicationContextBuilder to:

public class LambdaApplicationContextBuilder extends DefaultApplicationContextBuilder {
    public LambdaApplicationContextBuilderReplacement() {
        setLambdaConfiguration(this);
    }

    public static void setLambdaConfiguration(ApplicationContextBuilder builder) {
Boolean deduceEnvironment = (applicationContextBuilder instanceof DefaultApplicationContextBuilder) ?
((DefaultApplicationContextBuilder) applicationContextBuilder).getDeduceEnvironments().orElse(null) : null

        if (DefaultEnvironment.shouldDeduceEnvironment(deduceEnvironment)) {
            builder = builder.environments(Environment.FUNCTION, MicronautLambdaContext.ENVIRONMENT_LAMBDA);
        }
        builder.eagerInitConfiguration(true)
                .eagerInitSingletons(true);
    }
...
..
.