SELECT * FROM iasql_install('<modules>');
INSERT INTO security_group (description, group_name)
VALUES ('Lambda Security Group', '<sgGroupName>');
INSERT INTO security_group_rule (is_egress, ip_protocol, from_port, to_port, cidr_ipv4, description, security_group_id)
SELECT false, 'tcp', 80, 80, '0.0.0.0/0', '<prefix>lambda_rule_http', id
FROM security_group
WHERE group_name = '<sgGroupName>';
INSERT INTO security_group_rule (is_egress, ip_protocol, from_port, to_port, cidr_ipv4, description, security_group_id)
SELECT true, 'tcp', 1, 65335, '0.0.0.0/0', '<prefix>lambda_rule_egress', id
FROM security_group
WHERE group_name = '<sgGroupName>';
INSERT INTO iam_role (role_name, assume_role_policy_document, attached_policies_arns)
VALUES ('<lambdaFunctionRoleName>', '<attachAssumeLambdaPolicy>', array['<lambdaFunctionRoleTaskPolicyArn>', '<lambdaVpcFunctionRoleTaskPolicyArn>']);
BEGIN;
INSERT INTO lambda_function (name, zip_b64, handler, runtime, subnets, role_name)
VALUES ('<lambdaFunctionName>', '<lambdaFunctionCode>', '<lambdaFunctionHandler>', '<lambdaFunctionRuntime14>', (select array(select subnet_id from subnet inner join vpc on vpc.id = subnet.vpc_id where is_default = true and vpc.region = '<region>' limit 3)), '<lambdaFunctionRoleName>');
INSERT INTO lambda_function_security_groups (lambda_function_id, security_group_id)
VALUES ((SELECT id FROM lambda_function WHERE name = '<lambdaFunctionName>'), (select id from security_group where group_name = '<sgGroupName>' and region = '<region>' limit 1));
COMMIT;
SELECT *
FROM lambda_function
WHERE name = '<lambdaFunctionName>';
SELECT *
FROM lambda_function_security_groups
WHERE lambda_function_id=(SELECT id FROM lambda_function WHERE name = '<lambdaFunctionName>');
SELECT *
FROM invoke_lambda_function('<lambdaFunctionName>', '{"name": "test"}');
INSERT INTO vpc (cidr_block, tags, enable_dns_hostnames, enable_dns_support, region)
VALUES ('192.<randIPBlock>.0.0/16', '{"name":"<prefix>-1"}', true, true, '<region>');
INSERT INTO subnet (availability_zone, vpc_id, cidr_block, region)
SELECT (SELECT name FROM availability_zone WHERE region = '<region>' LIMIT 1), id, '192.<randIPBlock>.0.0/16', '<region>'
FROM vpc
WHERE cidr_block = '192.<randIPBlock>.0.0/16' and region='<region>' limit 1;
INSERT INTO security_group (description, group_name, vpc_id)
VALUES ('Lambda security group for non-default vpc', '<prefix>lambdanotdefault', (SELECT id FROM vpc WHERE cidr_block='192.<randIPBlock>.0.0/16' AND region='<region>' limit 1));
INSERT INTO security_group_rule (is_egress, ip_protocol, from_port, to_port, cidr_ipv4, description, security_group_id)
SELECT false, 'tcp', 80, 80, '0.0.0.0/0', '<prefix>lambda_rule_http_not_default', id
FROM security_group
WHERE group_name = '<prefix>lambdanotdefault';
INSERT INTO security_group_rule (is_egress, ip_protocol, from_port, to_port, cidr_ipv4, description, security_group_id)
SELECT true, 'tcp', 1, 65335, '0.0.0.0/0', '<prefix>lambda_rule_egress_not_default', id
FROM security_group
WHERE group_name = '<prefix>lambdanotdefault';
UPDATE lambda_function SET subnets = (select array(select subnet_id from subnet inner join vpc on vpc.id = subnet.vpc_id where vpc.region = '<region>' and subnet.cidr_block='192.<randIPBlock>.0.0/16'))
WHERE name = '<lambdaFunctionName>';
UPDATE lambda_function_security_groups SET security_group_id=(select id from security_group where group_name='<prefix>lambdanotdefault' and region='<region>' limit 1) where lambda_function_id=
(select id from lambda_function where name='<lambdaFunctionName>' AND region='<region>');
UPDATE lambda_function SET runtime = '<lambdaFunctionRuntime16>' WHERE name = '<lambdaFunctionName>';
UPDATE lambda_function SET zip_b64 = '<lambdaFunctionCodeUpdate>' WHERE name = '<lambdaFunctionName>';
UPDATE lambda_function SET tags = '{"updated": "true"}' WHERE name = '<lambdaFunctionName>';
BEGIN;
DELETE FROM lambda_function_security_groups
WHERE lambda_function_id = (SELECT id FROM lambda_function WHERE name = '<lambdaFunctionName>');
DELETE FROM lambda_function WHERE name = '<lambdaFunctionName>';
COMMIT;
DELETE FROM security_group_rule WHERE description='<prefix>lambda_rule_http' or description='<prefix>lambda_rule_egress' AND region='<region>';
DELETE FROM security_group WHERE group_name = '<sgGroupName>' AND region='<region>';
WITH vpc as (
SELECT id
FROM vpc
WHERE cidr_block = '192.<randIPBlock>.0.0/16' AND region='<region>' LIMIT 1
)
DELETE FROM subnet
USING vpc
WHERE subnet.vpc_id = vpc.id;
WITH vpc as (
SELECT id
FROM vpc
WHERE cidr_block = '192.<randIPBlock>.0.0/16' AND region='<region>' LIMIT 1
)
DELETE FROM route_table_association
USING vpc
WHERE route_table_association.vpc_id = vpc.id;
WITH vpc as (
SELECT id
FROM vpc
WHERE cidr_block = '192.<randIPBlock>.0.0/16' AND region='<region>' LIMIT 1
)
DELETE FROM route_table
USING vpc
WHERE route_table.vpc_id = vpc.id;
DELETE FROM security_group_rule WHERE description='<prefix>lambda_rule_http_not_default' or description='<prefix>lambda_rule_egress_not_default' AND region='<region>';
DELETE FROM security_group WHERE group_name = '<prefix>lambdanotdefault' AND region='<region>';
WITH vpc as (
SELECT id
FROM vpc
WHERE cidr_block = '192.<randIPBlock>.0.0/16' AND region='<region>' LIMIT 1
)
DELETE FROM security_group_rule
USING vpc
WHERE security_group_id = (
SELECT id
FROM security_group
WHERE security_group.vpc_id=vpc.id);
WITH vpc as (
SELECT id
FROM vpc
WHERE cidr_block = '192.<randIPBlock>.0.0/16' AND region='<region>' LIMIT 1
)
DELETE FROM security_group
USING vpc
WHERE security_group.vpc_id = vpc.id;
DELETE FROM vpc WHERE cidr_block='192.<randIPBlock>.0.0/16' AND region='<region>';