aws-amplify / amplify-backend

Home to all tools related to Amplify's code-first DX (Gen 2) for building fullstack apps on AWS
Apache License 2.0
158 stars 54 forks source link

Amplify Gen2 function missing Vpc property #1112

Open nikhil123987 opened 6 months ago

nikhil123987 commented 6 months ago

Environment information

export type FunctionProps = {
    /**
     * A name for the function.
     * Defaults to the basename of the entry path if specified.
     * If no entry is specified, defaults to the directory name in which this function is defined.
     *
     * Example:
     * If entry is `./scheduled-db-backup.ts` the name will default to "scheduled-db-backup"
     * If entry is not set and the function is defined in `amplify/functions/db-backup/resource.ts` the name will default to "db-backup"
     */
    name?: string;
    /**
     * The path to the file that contains the function entry point.
     * If this is a relative path, it is computed relative to the file where this function is defined
     *
     * Defaults to './handler.ts'
     */
    entry?: string;
    /**
     * An amount of time in seconds between 1 second and 15 minutes.
     * Must be a whole number.
     * Default is 3 seconds.
     */
    timeoutSeconds?: number;
    /**
     * An amount of memory (RAM) to allocate to the function between 128 and 10240 MB.
     * Must be a whole number.
     * Default is 128MB.
     */
    memoryMB?: number;
    /**
     * Environment variables that will be available during function execution
     */
    environment?: Record<string, string | BackendSecret>;
    /**
     * Node runtime version for the lambda environment.
     *
     * Defaults to the oldest NodeJS LTS version. See https://nodejs.org/en/about/previous-releases
     */
    runtime?: NodeVersion;
};

Description

Lambda function created using Amplify Gen2 do not have Vpc property just like other props such as timeoutSeconds, environment, memoryMB etc. defineFunction which is imported from @aws-amplify/backend module do not consists of Vpc configurations

Due to missing Vpc property, it is not possible to configure lambda function with VPC.

Workaround : Tried to override the lambda function resource by importing ec2.Vpc.fromVpcAttributes from 'aws-cdk-lib/aws-ec2', however It is not possible to override the lambda function as amplify expects the lambda function to be passed inside defineBackend. The below code snippet do not work :

const backend = defineBackend({});

const functionStack = backend.createStack('functionStack');

const myVPC = ec2.Vpc.fromVpcAttributes(functionStack, 'Gen2Vpc',{
  vpcId: 'vpc-1234',
  availabilityZones: ['eu-central-1a', 'eu-central-1b'],
});

const lambda = new NodejsFunction(functionStack, 'myLambda', {
  // ... other props, 
  vpc: myVPC
});
ykethan commented 6 months ago

Hey @nikhil123987, thank you for reaching out. Marking this as feature-request. to note you can utilize the Lambda function construct to build a custom function with a VPC.